首页 > 解决方案 > Swift firebase 我的数据库有点复杂我应该如何访问这个孩子?

问题描述

我使用 firebase 作为我的数据库。所以我的数据库变得复杂了,我被困在访问一个特定的孩子上。

我有这个片段:

for child in snapshot.childSnapshot(forPath: "rated").children{
             let child = child as! DataSnapshot

但在那之后我不知道该怎么办。

我的 json 结构如下所示:

-LfdhZk3UY9ag5zIYUgh
 rated
  2fLHYArJzCR0wg6waeW23n859pI3
   -Lff-x5TTeOn7ZO1ygDc
     UserId:"05E1OFKYsiWQf2s7LsEm6XM4oWy1"
   -Lff0DsKvs5lnxtFCQL_
     UserId:"Q6jYe9gzAePy8x6SJulTYLMQTpN2"

我想访问 2fLHYArJzCR0wg6waeW23n859pI3 下的所有 UserId。但我一直都是零,现在我迷路了

标签: swiftfirebasefirebase-realtime-database

解决方案


rated您的下方似乎有两个动态级别,您通常会通过遍历子级来处理它们。然后您可以再次访问属性 ( UserId) childSnapshot(forPath:)

所以像:

for child in snapshot.childSnapshot(forPath: "rated").children{
    for userSnapshot in child.children.allObjects as! [DataSnapshot] {
        for dynamicSnapshot in userSnapshot.children.allObjects as! [DataSnapshot] {
            let userID = dynamicSnapshot.childSnapshot(forPath: "UserId").value

推荐阅读