gpt4 book ai didi

swift - Firebase 在 Swift 中迭代快照

转载 作者:可可西里 更新时间:2023-11-01 01:37:15 24 4
gpt4 key购买 nike

我的应用程序中有这段代码:

DataService.dataService.USERS_REF.childByAppendingPath(DataService.dataService.CURRENT_USER_ID).childByAppendingPath("groups").observeSingleEventOfType(.Value, withBlock: { snapshot in

print(snapshot)

})

如果我打印结果,我只会得到这个值的快照:

Snap (groups) {
"-KBAX-d4UNQMZErrTqPD" = true;
"-KBAX0rrrR-trRZc408F" = true;
"-KBAX2-lrGd-Bx2zVizv" = true;
"-KBCL_xUjm-kf1JsGgpA" = true;
"-KBCOlullSmzi46ecf9J" = true;
}

如果我打印 snapshot.key 我得到

groups

如果我打印 snapshot.value 我会得到

{
"-KBAX-d4UNQMZErrTqPD" = true;
"-KBAX0rrrR-trRZc408F" = true;
"-KBAX2-lrGd-Bx2zVizv" = true;
"-KBCL_xUjm-kf1JsGgpA" = true;
"-KBCOlullSmzi46ecf9J" = true;
}

有没有办法遍历所有快照值并获取每个值的所有键和值?在本例中,此快照中有 5 个键和值。

最佳答案

没关系,我已经找到了解决方案。在这种情况下:

DataService.dataService.USERS_REF.childByAppendingPath(DataService.dataService.CURRENT_USER_ID).childByAppendingPath("groups").observeSingleEventOfType(.Value, withBlock: { snapshot in

for rest in snapshot.children.allObjects as! [FDataSnapshot] {
print(rest.key)
}
})

关于swift - Firebase 在 Swift 中迭代快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35582226/

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