gpt4 book ai didi

json - 如何从 firebase 中的键值对中获取唯一的键?

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

尝试想出一些逻辑来帮助记录 firebase 的一些资源。查找了有关该问题的一些资源,这个问题有点类似于该问题,但并未扩展到解决它的范围。 SimillarQuestion .而且 avilable 文档也没有提到任何实现它的逻辑 GoogleDoc .

如何只从给定的 firebase json 树中获取 key ?正如他们所说,一张图片讲述千言万语。已经得到下面提到的用于 firebase 设置的树。提到的是所需的输出,它在 TableView 中显示给用户。

--uniUsers
|
|Fruits
| |
| |Apple:true
| |Kiwi:true
| |Orange:true
|
|Veggies
|
|Carrot:true
|Onions:true
|Lettuce:true

快速代码

 func fetchTotalUsers(){
let tempUniId = "Fruits"
let refUniId = Database.database().reference(withPath: "UniUsers/\(tempUniId)")

refUniId.observe(.value, with: { (snapp) in

print("Value><", snapp.key)
print("Key><", snapp.value!)
}
}

快速输出:

  Value >< Fruits
Key >< {
Apple = 1;
Kiwi = 1;
Orange = 1;
}

要求的输出

  Apple
Kiwi
Orange

最佳答案

您可以使用 keys 属性。

来自官方文档here

keys

A collection containing just the keys of the dictionary.

再次来自官方文档:

When iterated over, keys appear in this collection in the same order as they occur in the dictionary’s key-value pairs. Each key in the keys collection has a unique value.

所以在你的特殊情况下你可以做这样的事情:

因为你从 firebase 服务器获取字典

refUniId.observe(.value, with: { (snapp) in

print("Value><", snapp.key)
print("Key><", snapp.value!)

if let dict = snapp.value as? [String: Any] {

for k in dict.keys {
print(k)
}
}
}

关于json - 如何从 firebase 中的键值对中获取唯一的键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47565172/

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