gpt4 book ai didi

ios - 检索 Firebase 数据

转载 作者:行者123 更新时间:2023-11-28 06:37:53 25 4
gpt4 key购买 nike

这是firebase数据树 enter image description here

有两个 parent ,每人有两个 child 。如何检索“性”的所有数据。

这是我的代码。

ref.child("Doctor").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
if let result = snapshot.children.allObjects as? [FIRDataSnapshot] {
for child in result {

print("Here 1")
print(child)
let gender = child.value!["sex"] as? String
print("Here 2")
//print("Sex")
print(gender)
}

} else {
print("no results")
}
}) { (error) in
print(error.localizedDescription)
}

当我打印性别的值时,它显示的是nil值。

最佳答案

您正试图跳过代码中的一个级别。您监听根节点的值,然后遍历其子节点。这将为您获取节点 Msm...eqn... 的快照。如果您检查这些节点内部,它们都没有子属性 sex

要解决这个问题,请在代码中再添加一个循环以获取推送 ID(以 -K 开头的键):

ref.child("Doctor").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
if let result = snapshot.children.allObjects as? [FIRDataSnapshot] {
for child in result {
for child2 in child.children {
let gender = child2.value!["sex"] as? String
print(gender)
}
}

} else {
print("no results")
}
}) { (error) in
print(error.localizedDescription)
}

关于ios - 检索 Firebase 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38671830/

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