我只能在我的应用程序上看到用户名,而不是年龄,但是我可以在编译器中看到两个值,但不确定是什么问题。
func fetchDataa(){
// var ref: DatabaseReference!
// ref = Database.database().reference()
guard let currentUid = Auth.auth().currentUser?.uid else {return}
Database.database().reference().child("users").child(currentUid).observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? Dictionary<String , AnyObject> else {return}
let uid = snapshot.key
let user = User(uid:uid , dictionary: dictionary)
print(Thread.current)
print(Thread.main)
self.userAgeLabel.text = user.userage
self.user = user
self.userAgeLabel.text = user.userage
self.user = user
print(snapshot.value)
// Get user value
// let value = snapshot.value as? NSDictionary
print(dictionary)
// ...
}) { (error) in
print(error.localizedDescription)
}
//这就是我将信息存储为字典的方式 //已存储用户信息
let userID = Auth.auth().currentUser?.uid
let userData = ["userName": userName,
"userAge ": userAge] as [String? : Any]
let ref = Database.database().reference()
ref.child("users/\(userID ?? "")").setValue(userData)
// ref.child("users").setValue(values)
}
//用户初始化程序类
班级用户{
var username:String!
var userage:String!
var uid:String!
init(uid:String,dictionary:Dictionary){
self.uid = uid
if let username = dictionary [ "userName" ] as? String{
self.username = username
}
if let userage = dictionary [ "userAge" ] as? String{
self.userage = userage
}
}
}
}
Firebase结构:
答案 0 :(得分:0)
你可以试试吗?
Database.database().reference().child("users").child(currentUid).observeEvent(of: .value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: Anyobject] {
let userAgeDictionary = dictionary["age"] as? String
print(userAgeDictionary)
这会打印出年龄吗?
答案 1 :(得分:0)
您已经在"
属性周围添加了额外的userAge
。因此,关键是"userAge"
包括双引号。
要从字典中获取该值:
if let userage = dictionary [ "\"userAge\"" ] as? String{
self.userage = userage
}
但是,今后您可能需要解决实际数据中的问题:在Firebase控制台中输入密钥时,由于密钥始终是字符串,因此不需要在其周围输入引号。