[英]Could not cast value of type 'NSTaggedPointerString' to 'NSDictionary'. Swift/Firebase
When listening to a new object in Firebase, I get this error: Could not cast value of type NSTaggedPointerString
to NSDictionary
. 在Firebase中收听新对象时,我收到此错误:无法将
NSTaggedPointerString
类型的值NSTaggedPointerString
为NSDictionary
。 Here is the code that listens to aa user that has registered. 以下是侦听已注册用户的代码。
FIRAuth.auth()!.addStateDidChangeListener { auth, user in
guard let user = user else { return }
self.user = User(authData: user)
let userref = FIRDatabase.database().reference(withPath: "users").child(self.user.uid)
userref.observe(.value, with: { snapshot in
print(snapshot.value!)
var newItems: [UserItem] = []
for item in snapshot.children {
let userDetail = UserItem(snapshot: item as! FIRDataSnapshot) /////////// CRASHES HERE //////////
newItems.append(userDetail)
}
self.userItem = newItems
})
print(self.user.uid)
}
And when the error comes up it points to here: 当错误出现时,它指向这里:
init(snapshot: FIRDataSnapshot) {
key = snapshot.key
let snapshotValue = snapshot.value as! [String:Any] ///// ERROR HERE
name = snapshotValue["name"] as! String
email = snapshotValue["email"] as! String
age = snapshotValue["age"] as! String
ref = snapshot.ref
}
The console is printing what is inside Firebase correctly and the data is there and it exists, but this casting or data read error keeps happening. 控制台正在打印Firebase内部的内容并且数据存在且存在,但此转换或数据读取错误仍在继续。 It has something to do with the conversion but I'm not exactly sure.
它与转换有关,但我不确定。
Say 说
snapshot.value as! String
In this case, the value is a string, so that is what you must cast to. 在这种情况下,值是一个字符串,因此您必须转换为。
可能是您更新数据库并且旧数据格式不正确,尝试删除数据库,添加新数据并再次运行程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.