我正在尝试使用 swift 代码从 firebase 获取当前用户的名字。下面是我的 JSON 文件和快速代码。但这不是检索名字。请建议
快速代码:
if FIRAuth.auth()?.currentUser != nil{
let queryRef = FIRDatabase.database().reference().child("expatsappmembers")
let userID : String = (FIRAuth.auth()?.currentUser?.uid)!
print("Current user ID is" + userID)
queryRef.child("uid").child(userID).observeSingleEvent(of: .value, with: {(snapshot) in
print(snapshot.value)
let userName = (snapshot.value as! NSDictionary)["firstname"] as! String
print(userName)
})
}
最佳答案
在 Swift 3 中从 Firebase 获取值略有不同
正确的做法是:
ref.child("expatsappmembers").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let firstName = value?["firstname"] as? String
// ...
}) { (error) in
print(error.localizedDescription)
}
关于ios - 如何使用 Swift 从 Firebase 中的当前用户检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44001903/