I'm trying to get a specific value from Firebase Database. I looked some of the documents such as Google's, but I couldn't do it. Here is the JSON file of the database:
我试图从Firebase数据库获得一个特定的值。我看了一些文件,比如谷歌,但是我做不到。这是数据库的JSON文件:
{
"Kullanıcı" : {
"ahmetozrahat25" : {
"E-Mail" : "ahmetozrahat25@gmail.com",
"Yetki" : "user"
},
"banuozrht" : {
"E-Mail" : "banuozrahat@gmail.com",
"Yetki" : "user"
}
}
}
Swift Code:
斯威夫特代码:
ref?.child("Kullanıcı").child(userName.text!).observeSingleEvent(of: .value, with: { (snapshot) in
if let item = snapshot.value as? String{
self.changedName = item
}
})
I want to get E-Mail
value of a user, not everybody's. How can I do that?
我想获得用户的电子邮件价值,而不是每个人的。我怎么做呢?
3 个解决方案
#1
4
At last I found a solution. If I declare a var and try to use later it returns nil, but if I try use snapshot.value as? String
it's ok. Here is a example I did.
最后我找到了一个解决办法。如果我声明一个var并尝试稍后使用它会返回nil,但是如果我尝试使用snapshot。值吗?字符串没关系。这是我做的一个例子。
ref: FIRDatabaseReference?
handle: FIRDatabaseHandle?
let user = FIRAuth.auth()?.currentUser
ref = FIRDatabase.database().reference()
handle = ref?.child("Kullanıcı").child((user?.uid)!).child("Yetki").observe(.value, with: { (snapshot) in
if let value = snapshot.value as? String{
if snapshot.value as? String == "admin"{
self.items.append("Soru Gönder")
self.self.tblView.reloadData()
}
}
})
#2
3
In your code, the snapshot will contain a dictionary of child values. To access them, cast the snapshot.value as a Dictionary and then accessing the individual children is a snap (shot, lol)
在代码中,快照将包含子值字典。要访问它们,请选择快照。值作为一个字典,然后访问各个子元素是一个简单的过程(shot, lol)
ref?.child("Kullanıcı").child(userName.text!)
.observeSingleEvent(of: .value, with: { (snapshot) in
let userDict = snapshot.value as! [String: Any]
let email = userDict["E-Mail"] as! String
let yetki = userDict["Yetki"] as! String
print("email: \(email) yetki: \(yetki)")
})
#3
2
Add the "E-Mail" child to the query.
将“电子邮件”子元素添加到查询中。
ref?.child("Kullanıcı").child(userName.text!).child("E-Mail").observeSingleEvent(of: .value, with: { (snapshot) in
if let item = snapshot.value as? String{
self.changedName = item
}
})
#1
4
At last I found a solution. If I declare a var and try to use later it returns nil, but if I try use snapshot.value as? String
it's ok. Here is a example I did.
最后我找到了一个解决办法。如果我声明一个var并尝试稍后使用它会返回nil,但是如果我尝试使用snapshot。值吗?字符串没关系。这是我做的一个例子。
ref: FIRDatabaseReference?
handle: FIRDatabaseHandle?
let user = FIRAuth.auth()?.currentUser
ref = FIRDatabase.database().reference()
handle = ref?.child("Kullanıcı").child((user?.uid)!).child("Yetki").observe(.value, with: { (snapshot) in
if let value = snapshot.value as? String{
if snapshot.value as? String == "admin"{
self.items.append("Soru Gönder")
self.self.tblView.reloadData()
}
}
})
#2
3
In your code, the snapshot will contain a dictionary of child values. To access them, cast the snapshot.value as a Dictionary and then accessing the individual children is a snap (shot, lol)
在代码中,快照将包含子值字典。要访问它们,请选择快照。值作为一个字典,然后访问各个子元素是一个简单的过程(shot, lol)
ref?.child("Kullanıcı").child(userName.text!)
.observeSingleEvent(of: .value, with: { (snapshot) in
let userDict = snapshot.value as! [String: Any]
let email = userDict["E-Mail"] as! String
let yetki = userDict["Yetki"] as! String
print("email: \(email) yetki: \(yetki)")
})
#3
2
Add the "E-Mail" child to the query.
将“电子邮件”子元素添加到查询中。
ref?.child("Kullanıcı").child(userName.text!).child("E-Mail").observeSingleEvent(of: .value, with: { (snapshot) in
if let item = snapshot.value as? String{
self.changedName = item
}
})