我正在尝试从拥有的Firebase数据库中获取数据,而在获取数据时,应用程序因以下错误而崩溃:
Fatal error: Unexpectedly found nil while unwrapping an Optional value
我看了一下,但什么也没找到,打印postDict
或snap
看起来不错,除了一件事之外,但我无法确切得到零。
这是我的代码:
func fetchPosts() {
ref.child("posts")
ref.observeSingleEvent(of: .value) { (snapshot) in
for post in snapshot.children {
let snap = post as! DataSnapshot
let postDict = snap.value as! [String: Any]
let imageUrl = postDict["imageUrl"] as! String
let numberOfDislikes = postDict["numberOfDislikes"] as! Int
let numberOfLikes = postDict["numberOfLikes"] as! Int
let postCreator = postDict["postCreator"] as! String
let postTitle = postDict["title"] as! String
self.fetchedPosts.append(Post(title: postTitle, image: imageUrl, postCreator: postCreator, numberOfLikes: numberOfLikes, numberOfDisLikes: numberOfDislikes))
}
}
}
这是打印的日志:
[]
Snap (posts) {
"D190D473-65B1-4D65-84B9-C7F690367AE8" = {
imageUrl = "https://firebasestorage.googleapis.com/v0/b/projectx-92e98.appspot.com/o/739AABC4-2AEC-4111-8890-63CDEC6F8995?alt=media&token=3b447641-9ec3-48e2-8d2c-92396b57d64e";
numberOfDislikes = 0;
numberOfLikes = 0;
postCreator = UaQpxKIjRYNLvyAXvFbUCpc8n5g2;
title = 'titleTest';
};
}
崩溃似乎在这一行:
let imageUrl = postDict["imageUrl"] as! String