I have price list of ingredients, stored in Cloud Firestore. I want to calculate price for specific recepie, pass it to another VC and apply to label, so I wrote this code:
extension RecepiesViewController: UICollectionViewDelegate {
//Passing data to next VC
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! ResultViewController
destinationVC.result = totalPrice
}
//Recepie cell pressed
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("You selected cell #\(indexPath.item)!")
// Calculating price for recepie
func recepieCalculation(_ completion: @escaping (Double) ->()) {
for ingridients in cakeRecepies![indexPath.item].ingridientList {
db.collection("Okey")
.document(ingridients.name)
.getDocument() { (document, error) in
if let document = document, document.exists {
let data = document.data()
if let price = data!["price"] as? Double, let count = data!["count"] as? Int {
let priceForOne = price / Double(count)
self.totalPrice = self.totalPrice + priceForOne * Double(ingridients.count)
}
} else {
print("Document does not exist")
}
completion(self.totalPrice)
}
}
}
recepieCalculation {totalPrice in
self.performSegue(withIdentifier: "goToResult", sender: self)
}
}
}
Another VC code:
import Foundation
import UIKit
import RealmSwift
class ResultViewController: UIViewController {
@IBOutlet weak var resultLabel: UILabel!
var result: Double = 0.0 {
didSet {
print(result)
resultLabel.text = String(format: ".%f", result)
}
}
@IBOutlet weak var recepieImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
}
And at line resultLabel.text = String(format: ".%f", result)
got error
Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
What could be wrong?
This is a common mistake.
At the moment the destination controller of the segue is going to be presented the outlets are not connected yet, so assigning the value to the label in the didSet
observer crashes.
Solution:
Delete the observer
var result = 0.0
and assign the value in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
print(result)
resultLabel.text = String(format: ".%f", result)
}