ios - 如果将方法放入 viewDidLoad() 中,为什么应用程序会崩溃?

标签 ios swift firebase

我在 viewDidLoad() 中放置了一个方法,当应用程序运行时它运行良好。它从数据库读取数据并将数据写入数据库。但是,当我进入那个特定的 View Controller 时,它崩溃了!

错误:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

但它并不为零,如下图所示

为什么会崩溃?是因为我看到它才加载吗?

编辑

这是放置在viewDidLoad中的方法:

let historyRef = ref.child("history")
    historyRef.observe(.value) { (snapshot) in
        if snapshot.exists() {
            for history in snapshot.children {
                let snap = history as! FIRDataSnapshot
                let dict = snap.value as! [String: Any]
                print("dict: \(dict)")
                let historyKey = snap.key
                print("History Key: \(historyKey)")

                    // fetch ride information ......

                let historyDB : FIRDatabaseReference = self.ref.child("history").child(historyKey)
                historyDB.observe(.value) { (snapshot) in
                    if snapshot.exists() {
                        let rideId = snap.key // ... rideId
                        print("rideId: \(rideId)")

                        var distance = dict["distance"] as! Double // distance
                        distance = Double(round(1000 * distance)/1000)

                        let riderPaid = dict["riderPaid"] as! Bool // riderPaid
                        var ride_price = dict["ride_price"] as! Double // price before fees
                        var ryyde_fees = dict["ryyde_fees"] as! Double // ryyde_fees
                        var driver_payout = dict["driver_payout"] as! Double // driver_payout

                        if  riderPaid == true {
                            ride_price = Double(round(1000 * ride_price)/1000)
                            print("Ride Price: $ \(ride_price)")
                            ryyde_fees = Double(round(1000 * (ride_price * 0.25))/1000)
                            print("Ryyde Fees: $ \(ryyde_fees)")
                            driver_payout = Double(round(1000 * (ride_price - ryyde_fees))/1000)
                            print("Driver Payout: $ \(driver_payout)")

                            self.balance = self.balance + driver_payout
                            self.txtBalance.text = (String(format: "$ %.2f", self.balance))

                        }
                    }
                }
            }
        }
    }

崩溃的行是:

var distance = dict["distance"] as! Double // distance
distance = Double(round(1000 * distance)/1000)

如果我注释掉这一行,那么它会在下一行崩溃......

results

最佳答案

做了一些更改,这似乎有效:

 let riderPaid: Bool = (dict["riderPaid"] != nil)// riderPaid
                        print("riderPaid: \(riderPaid)")
                        var ride_price: Double = dict["ride_price"] as? Double ?? 0.0  // price before fees
                        var ryyde_fees: Double = dict["ryyde_fees"] as? Double ?? 0.0 // ryyde_fees
                        var driver_payout: Double = dict["driver_payout"] as? Double ?? 0.0 // driver_payout

关于ios - 如果将方法放入 viewDidLoad() 中,为什么应用程序会崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53660339/

相关文章:

firebase - 如何从cloud firestore获取数据,其中user.uid等于flutter中的文档ID?

javascript - 使用 PFQuery 检索数组中包含特定项目的 PFInstallation 对象?

ios - UITesting 获取事件应用程序列表失败

iOS UIView动画changeFrame坐标系错误

ios - 如何从 didSelectRowAtindexPath : function 访问单元格 imageView

java - 如何从 Firebase 存储获取 URL getDownloadURL

ios - UITextView 偏移文本的方式与 UILabel 不同

ios - Realm swift : No such module 'RealmSwift'

swift - 在swift中获取枚举类型的值

swift - Alamofire 请求出现错误 'Extra Argument in call'