gpt4 book ai didi

ios - 在 Firebase (Swift) 中获取所有用户的同一个 child

转载 作者:搜寻专家 更新时间:2023-11-01 07:21:53 24 4
gpt4 key购买 nike

我目前正在 Firebase 中构建一个应用程序,我希望获得所有用户菠萝率。

这是树:

user1: uid1 {
fruits: {
apple: 3
pineapple: 5
}
}

user2: uid2 {
fruits: {
apple: 4
pineapple: 2
}
}

我试过这段代码:

var ref = FIRDatabase.database().reference()
ref.queryOrderedByChild("fruits")
.observeSingleEventOfType(.Value, withBlock: { (snapshot) in
if ( snapshot.value is NSNull ) {
print("not found")
} else {
for child in snapshot.children {
let rate = child.value["pineapple"] as! String
print(rate)
}
}
})

但它给我一个解包 Nil 的错误。

结构图:

enter image description here

最佳答案

应该是这样的

  var ref = FIRDatabase.database().reference()
ref.queryOrderedByChild("user2")
.observeSingleEventOfType(.Value, withBlock: { (snapshot) in
if snapshot.exists() {
for child in (snapshot.value?.allValues)! {
if let fruits = child["Fruits"] as? [String:String]{
let rate = fruits["pineapple"]
print(rate)
}
}
}
})

关于ios - 在 Firebase (Swift) 中获取所有用户的同一个 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38478417/

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