gpt4 book ai didi

ios - 如何将 Firebase 快照转换为项目数组?

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

{ 
"body": "Hello there",
"viewers": {
"1": {
"user_id": 1
},
"2":{
"user_id": 2
}
}
}

以上是我从 Firebase 收到的快照。

        fireRef!.observeEventType(FEventType.Value) { [weak self] (snapshot: FDataSnapshot!) -> Void in
if let stuff = snapshot.value as? [String:AnyObject] {
print(stuff) //okay, it prints the snapshot correctly
//Now, I need to access the "viewers" part, but how?
}
}

如何遍历每个查看器并打印用户 ID?

最佳答案

打开我上一个使用 Firebase 的项目,我按如下方式解决了这个问题:

if let viewers = stuff["viewers"] as? [String:[String:Int]] { //Firebase could deliver your numbers as strings, in that case try [String:[String:String]]
for viewer in viewers {
let userID = viewer.1
print(userID.values.first)
}
}

可能有更简洁的方法来做到这一点

关于ios - 如何将 Firebase 快照转换为项目数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36610001/

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