你好,我是Swift的新手,我遇到了问题。我有存储在Cloud Firestore中的配料价格表。我想计算特定Recepie的价格并将其传递给另一个VC,因此我编写了以下代码:
extension RecepiesViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
func recepieCalculation(_ completion: @escaping (Double) ->()) {
var totalPrice = 0.0
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)
totalPrice = totalPrice + priceForOne * Double(ingridients.count)
}
} else {
print("Document does not exist")
}
completion(totalPrice)
}
}
}
recepieCalculation {totalPrice in
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! ResultViewController
destinationVC.result = totalPrice
}
self.performSegue(withIdentifier: "goToResult", sender: self)
}
}
}
第二个VC代码:
class ResultViewController: UIViewController {
var result: Double? {
didSet {
print(result)
}
}
@IBOutlet weak var recepieImage: UIImageView!
override func viewDidLoad() {
}
}
但是控制台上没有任何内容。有什么问题吗?
获取
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! ResultViewController
destinationVC.result = totalPrice
}