使用Swift从Firebase中的所有孩子获取数据 [英] Get the data from all children in firebase using swift
本文介绍了使用Swift从Firebase中的所有孩子获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个firebase实时数据库.看起来像这样:
I have a firebase realtime database. It looks like this:
这是我的代码:
ref.child("2").observeSingleEvent(of: .value, with: { snapshot in
guard let dict = snapshot.value as? [String:Any] else {
print("Error")
return
}
let latitude = dict["Latitude"] as Any
let longtitude = dict["Longtitude"] as Any
print(longtitude)
print(latitude)
})
我的问题是我的代码仅从名为2
的孩子那里检索数据.如何使它从所有孩子那里检索数据?
My problem is that my code retrieves the data from only the child called 2
. How can I make it retrieve the data from all the children?
如果您有任何疑问,请告诉我.感谢您的帮助!
If you have any questions just let me know. Thanks for any help!
推荐答案
您需要在JSON中将观察者附加上一层,然后循环遍历子节点:
You'll want to attach the observer one level higher in the JSON, and then loop over the child nodes:
ref.observeSingleEvent(of: .value) { snapshot in
for case let child as FIRDataSnapshot in snapshot.children {
guard let dict = child.value as? [String:Any] else {
print("Error")
return
}
let latitude = dict["Latitude"] as Any
let longtitude = dict["Longtitude"] as Any
print(longtitude)
print(latitude)
}
}
取自在Firebase中对快照子代进行迭代的循环语法,请参阅>如何循环播放所有内容一次在同一循环中找到Firebase孩子?和在Firebase中循环
这篇关于使用Swift从Firebase中的所有孩子获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文