我正在尝试从Firebase数据库中获取特定值.我看了一些像谷歌这样的文件,但我做不到.这是数据库的
JSON文件:
{ "Kullanıcı" : { "ahmetozrahat25" : { "E-Mail" : "ahmetozrahat25@gmail.com","Yetki" : "user" },"banuozrht" : { "E-Mail" : "banuozrahat@gmail.com","Yetki" : "user" } } }
SWIFT代码:
ref?.child("Kullanıcı").child(userName.text!).observeSingleEvent(of: .value,with: { (snapshot) in if let item = snapshot.value as? String{ self.changedName = item } })
解决方法
在您的代码中,快照将包含子值的字典.要访问它们,请将snapshot.value转换为Dictionary,然后访问各个子项是一个快照(镜头,哈哈)
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)") })