Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将值(使用Firestore数据计算)应用于标记触发器;展开可选值";崩溃_Ios_Swift_Firebase_Google Cloud Firestore_Uilabel - Fatal编程技术网

Ios 将值(使用Firestore数据计算)应用于标记触发器;展开可选值";崩溃

Ios 将值(使用Firestore数据计算)应用于标记触发器;展开可选值";崩溃,ios,swift,firebase,google-cloud-firestore,uilabel,Ios,Swift,Firebase,Google Cloud Firestore,Uilabel,我有原料的价格表,存放在Cloud Firestore。我想计算特定收据的价格,将其传递给另一个VC并应用于label,因此我编写了以下代码: extension RecepiesViewController: UICollectionViewDelegate { //Passing data to next VC override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let des

我有原料的价格表,存放在Cloud Firestore。我想计算特定收据的价格,将其传递给另一个VC并应用于label,因此我编写了以下代码:

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)
        }
    }
}
另一个VC代码:

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()
    }
}
在第
resultLabel.text=String行(格式:“.%f”,结果)
出现错误

线程1:致命错误:隐式执行时意外发现nil 展开可选值


有什么问题吗?

这是一个常见的错误

当segue的目标控制器即将出现时,出口尚未连接,因此将值分配给
didSet
中的标签会崩溃

解决方案:

  • 删除观察员

    var result = 0.0 
    
  • 并在
    viewDidLoad

    override func viewDidLoad() {
        super.viewDidLoad()
        print(result)
        resultLabel.text = String(format: ".%f", result)
    }
    

  • 这是一个常见的错误

    当segue的目标控制器即将出现时,出口尚未连接,因此将值分配给
    didSet
    中的标签会崩溃

    解决方案:

    • 删除观察员

      var result = 0.0 
      
    • 并在
      viewDidLoad

      override func viewDidLoad() {
          super.viewDidLoad()
          print(result)
          resultLabel.text = String(format: ".%f", result)
      }
      

    这是否回答了您的问题?看起来您的标签插座未连接到情节提要。1。不,我没有任何强力开封器“!”。2.标签已连接,请检查它twice@MaxGavrilov,
    1。不,我没有任何强制展开程序“!”
    -false:
    @IBOutlet弱var resultLabel:UILabel此外,在调用
    viewDidLoad
    之前,所有插座均为
    nil
    。因此,如果在
    viewDidLoad
    之前调用
    result
    didSet
    ,那么
    resultLabel
    nil
    。这是否回答了您的问题?看起来您的标签插座未连接到情节提要。1。不,我没有任何强力开封器“!”。2.标签已连接,请检查它twice@MaxGavrilov,
    1。不,我没有任何强制展开程序“!”
    -false:
    @IBOutlet弱var resultLabel:UILabel此外,在调用
    viewDidLoad
    之前,所有插座均为
    nil
    。因此,如果在
    viewDidLoad
    之前调用
    result
    didSet
    ,则
    resultLabel
    nil