我在我的项目中使用 Firebase 实时数据库已经有一段时间了。最近我添加了 Firestore 的 pod 以将其与其他数据库一起使用,我的所有查询以及仅实时数据库的查询都出现以下错误:“‘下标’的使用不明确”。
如果我卸载 pod,一切都会恢复正常,绝对没有错误。 这就是我之前使用代码的方式:(工作)
func retriveObjects() {
ref.child("Dinner").queryOrderedByKey().observeSingleEvent(of: .value) { (snapshot) in
let allPosts = snapshot.value as! [String : AnyObject]
for (_, item) in allPosts {
if let itemCount = item["itemCount"] as? String {
print(itemCount)
}
}
}
}
现在这一行给我错误:
if let itemCount = item["itemCount"] as? String
除了保留两个数据库外,我该怎么做才能让它再次工作?
最佳答案
如评论中所述,快照的值被转换为 [String: AnyObject] 的字典,然后您尝试将 item 用作字典,但它被转换为 AnyObject(不是字典) .换句话说,item["itemCount"] 不适用于 AnyObject。
目前还不清楚是什么引发了这个错误,但我们有很多项目同时存在这两种情况,但并没有引发那个错误。您可能需要考虑稍微更改代码;
self.ref.child("Dinner").queryOrderedByKey().observeSingleEvent(of: .value) { (snapshot) in
let allPosts = snapshot.children.allObjects as! [DataSnapshot]
for post in allPosts {
let itemCount = post.childSnapshot(forPath: "itemCount").value as? String ?? "No Count"
print(itemCount)
}
}
关于swift - 将 Firestore 添加到我的项目会导致我的实时数据库查询出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56050803/