![](/img/trans.png)
[英]swift Xcode: unexpectedly found nil while unwrapping an Optional value (lldb)
[英]Xcode says found nil, but actual value is not nil
我正在嘗試從 Firebase Firestore 中獲取一個值,但由於某種原因,xcode 說它在打開一個可選項時發現 nil 。 如果它為 nil,我放置了一個檢查以顯示其他內容,但它仍然在我的if
中,我不知道為什么。
這是我的代碼:
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")
}
}
任何幫助將不勝感激!
我相信這條線:
let points = document.data()!["points"]
不針對nil
進行檢查。
如果document.data()
為nil
怎么辦?
另一種可能性是您的pointsLabel
。 如果您將 IBOutlet 用於 label 並且在加載視圖之前調用 Firebase 回調,則可能會發生類似的錯誤。
但是您應該讓我們知道錯誤發生在哪一行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.