本文介绍了从autoID节点中检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to get the values from within a autoID node. Below is my structure:

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

如何获取"reps","set"和"weight"的值.

How can I get the values of "reps", "set" and "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()路径.如何访问该节点?

I am aware I am missing the .child() path for the autoID node following .child("Bench Press"). How can I access this node?

预先感谢

推荐答案

那怎么办?

refSets
  .child("workout")
  .child("exercise")
  .child("Bench Press").observe(.value, with: {(snapshot) in
    guard let values = snapshot.value as [String: Any] else {
      return
    }

    for (key, value) in values {
      guard let objDict = value as? [String: Any],
        let reps = objDict["reps"] as? Double /*not sure maybe Int*/,
        let set = objDict["set"] as? Double,
        let weight = objDict["weight"] as? Double else {
        continue
      }
      let bp = BenchPress(reps: reps, set: set, weight: weight)

    }
  })

struct BenchPress {

  private let reps: Double
  private let set: Double
  private let weight: Double

  init(reps: Double, set: Double, weight: Double) {

    self.reps = reps
    self.set = set
    self.weight = weight
  }
}

这篇关于从autoID节点中检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 01:49