简体   繁体   English

Xcode 说 found nil,但实际值不是 nil

[英]Xcode says found nil, but actual value is not nil

I am trying to get a value from Firebase Firestore, but for some reason, xcode says that it found nil while unwrapping an optional.我正在尝试从 Firebase Firestore 中获取一个值,但由于某种原因,xcode 说它在打开一个可选项时发现 nil 。 I put a check in place to display something else if it is nil, but it still goes in my if , and I am not sure why.如果它为 nil,我放置了一个检查以显示其他内容,但它仍然在我的if中,我不知道为什么。

Here is my code:这是我的代码:

        let db = Firestore.firestore()
        let docRef = db.collection("Users").document(Auth.auth().currentUser!.uid)
        docRef.getDocument { (document, error) in
            if let document = document, document.exists {
                let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
                let points = document.data()!["points"]
                print(points)
                if (points != nil) {
                    self.pointsLabel.text = "\(points!)"
                }
                else {
                    self.pointsLabel.text = "There was an error getting your points"
                }
            } else {
                print("Document does not exist")
            }
        }

Any help would be greatly appreciated!任何帮助将不胜感激!

I believe this line:我相信这条线:

let points = document.data()!["points"]

is not checked against nil .不针对nil进行检查。

What if document.data() is nil ?如果document.data()nil怎么办?

Another possibility is your pointsLabel .另一种可能性是您的pointsLabel If you are using IBOutlet for the label and your Firebase callback is called before the view is load, then the error like that may happen.如果您将 IBOutlet 用于 label 并且在加载视图之前调用 Firebase 回调,则可能会发生类似的错误。

But you should let us know at which line the error happened.但是您应该让我们知道错误发生在哪一行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 迅捷的Xcode:解开可选值(lldb)时意外发现nil - swift Xcode: unexpectedly found nil while unwrapping an Optional value (lldb) XCode 8:展开包装可选值时意外发现nil - XCode 8: Unexpectedly found nil while unwrapping an optional value 展开非零值时发现为零 - Found nil when unwrapping non nil value Xcode Swift:appDelgate.window! is nil :致命错误:在解开可选值时意外发现 nil - Xcode Swift : appDelgate.window! is nil : Fatal error: Unexpectedly found nil while unwrapping an Optional value mapView.addAnnotation()说:“在展开可选值时意外找到nil” - mapView.addAnnotation() says “unexpectedly found nil while unwrapping an Optional value” 它在tableview的cellforrowatindexpath中崩溃,表示:“致命错误:在展开可选值时意外发现nil” - It crashes in cellforrowatindexpath of tableview , says : “fatal error: unexpectedly found nil while unwrapping an Optional value” 在iOS中,继续收到“在解包可选值时意外发现nil”的致命错误 - Keep getting a fatal error that says “unexpectedly found nil while unwrapping an Optional value” in iOS 在实际设备上的Glance中访问IBOutlet时,WatchKit“在解开可选值时意外发现nil” - WatchKit “unexpectedly found nil while unwrapping an Optional value” when accessing IBOutlet in Glance on actual device 值不是nil,但是在展开Optional值时意外发现nil - value is not nil but getting unexpectedly found nil while unwrapping an Optional value AVPlayerLayer 总是说 nil - AVPlayerLayer says always nil
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM