[英]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.