从儿童Firebase访问值 [英] Accessing values from Children Firebase
问题描述
我确实不太了解Swift 3.0 Firebase语法的更新,但是从快照子级检索值中的大多数似乎都是不可能的.为此,我使用如下代码段:
I am really not quite understanding the updates to the Swift 3.0 Firebase syntax, but most of all retrieving values from children of a snapshot seems to be impossible. To do so, I use a snippet like this:
if let snapVal = snapshot.value as? [String: AnyObject] {
for c in snapshot.children {
let cx = (c as! AnyObject)
let name = cx["NAME"] as! String
}
}
我已经采取了许多方法,但是FIRDatabaseSnapshot在新的Swift 3更新中有很多限制,并且AnyObject不允许从对象读取值,并且类似NSDictionary的类型也没有孩子.非常感谢所有帮助,谢谢!
I have taken many approaches to this but FIRDatabaseSnapshot has many restrictions in the new Swift 3 update, and AnyObject does not allow for values to be read from the object and NSDictionary-like types do not have children either. All help is very appreciated, thank you!
推荐答案
您收到的类型为FIRDataSnapshot
的snapShot实际上是符合 NSObject
的自定义类,因此只有符合以下条件的变量到FIRDataSnapshot
可以访问FIRDataSnapshot
提供的自定义功能,例如 .children
.
The snapShot that you receive of type FIRDataSnapshot
is actually a custom class conforming to NSObject
, so only the variables conforming to FIRDataSnapshot
can access the custom function that FIRDataSnapshot
provides such as .children
.
但是,当您访问一个值为snap.value
解析为 [String:AnyObject] 的新变量时,基本上它成为NSDictionary,而NSDictionary没有任何参数.children
.
But when you are accessing a new variable whose value is snap.value
parsed as [String:AnyObject], basically it becomes a NSDictionary, and NSDictionary doesn't has any parameter .children
.
FIRDatabase.database().reference().child("your_Ref").observeSingleEvent(of: .value, with: {(snap) in
for each in snap.children{
print(each)
}
if let snapDict = snap.value as? [String:AnyObject]{
for each in snapDict{
let keyID = each.key
let childValue = each.value["NAME"] as! String
}
}
})
这篇关于从儿童Firebase访问值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!