首页 > 解决方案 > 从 autoID 节点中检索值

问题描述

我想从 autoID 节点中获取值。下面是我的结构:

"workout"
  "exercise"
    "Bench Press"
      "(AutoIDNode)"
        "reps: 10"
        "set: 1"
        "weight: 60"

如何获得“reps”、“set”和“weight”的值。

这是我的快速代码:

refSets.child("workout").child("exercise").child("Bench Press").observe(DataEventType.value, with: {(snapshot) in
        if snapshot.childrenCount > 0 {
            self.setsData.removeAll()

            for sets in snapshot.children {
                let snap = sets as! DataSnapshot
                let key = snap.key
                let value = snap.value
                print(key)
                print(value!)
            }     
        }
    })

我知道我缺少 .child("Bench Press") 之后的 autoID 节点的 .child() 路径。如何访问该节点?

提前致谢

标签: swiftfirebase

解决方案


你可以试试

refSets.child("workout").child("exercise").child("Bench Press").observeSingleEvent(of: .value, with: {(snapshot) in

    if let res = snapshot.value as? [String:[String:Int]] {

        for item in Array(res.values) {

            print(item["reps"])
        }

    }

})

推荐阅读