【发布时间】:2020-11-05 07:54:09
【问题描述】:
我是 swift 和 firebase 的新手,我想知道为什么我会收到 snapshot.value 作为? NSDictionary 为 nil,即使用户已登录。
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
print(snapshot.value as? NSDictionary)
if let value = snapshot.value as? NSDictionary{
self.title = value["firstname"] as? String
print("o")
}
print(self.title)
}, withCancel: { (error) in
print(error.localizedDescription)
})
}
//I want it to return the name of the user (Ben) as the title of the navigation bar, but instead it returns nil
数据库是:
{
"users" : {
"-MBzED86cdVbvBNcYdpY" : {
"email" : "bb@gmail.com",
"firstname" : "benjamin",
"username" : "bb"
},
"-MBzEWEQ3cX6d5aJOByR" : {
"email" : "bslou.row@gmail.com",
"firstname" : "bslou",
"username" : "bbbslou.row"
}
}
}
当我打印快照时,我的程序返回“Snap (mDNGl4fQXEMx1wPnjGuhILp5f8j1)”
【问题讨论】:
-
print(type(of: snapshot.value))显示什么?并避免在 Swift 中使用NS...集合类型。 -
它显示
FIRDataSnapshot,好的,我将更改为字典 -
请编辑问题以显示您希望此查询返回的数据。由于看不到数据库的内容,也看不到uid的具体值,所以也无能为力。查询的结果只是告诉你在你查询的位置没有数据。
-
我编辑了@DougStevenson 的问题,我也展示了数据库
-
当我打印快照时,我只得到 id
标签: ios swift firebase firebase-realtime-database firebase-authentication