gpt4 book ai didi

swift - Firebase swift 4 : iterate through observe results

转载 作者:行者123 更新时间:2023-11-30 10:34:33 33 4
gpt4 key购买 nike

我有这个数据

enter image description here

我需要检索节点类别的数据。我尝试了这段代码

database?.child("restaurants/erbil/Today/category").observe(.childAdded, with: { (snapshot) in
for child in snapshot.children.allObjects as! [DataSnapshot] {
print(" output \(String(describing: child.value))")
}
})

但我只会返回 Burgers 节点的数据这是输出

enter image description here

最佳答案

您当前的代码循环遍历类别,为您提供 DataSnapshot object对于每个类别。然后,您需要从每个子快照中获取单独的 key 和属性,例如:

database?.child("restaurants/erbil/Today/category").observe(.childAdded, with: { (snapshot) in
for child in snapshot.children.allObjects as! [DataSnapshot] {
print(" key \(child.key)")
print(" name \(child.childSnapshot(forPath: "catName").value)")
}
})

关于swift - Firebase swift 4 : iterate through observe results,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58358379/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com