我正在尝试迭代以获取所有具有“ 00001” ID的子代,然后为每个子代获取“ timeDetected”的值。现在我有了快速代码:
"(^|, )(dog|cat)(,|$)"
这会将每个孩子输出到控制台,如下所示:
for item in snapshot.children {
print(item)
}
我该如何只打印出“ timeDetected”的值而不是整个快照,以便控制台显示如下:
Snap (fouihbnfqwubfwqouyb) {
timeDetected = "2018-05-28T16:00:13Z";
}
Snap (fouihbnfqwubtgowqouyb) {
timeDetected = "2018-05-28T16:00:18Z";
}
Snap (fouohbnfqwubtgowqouyb) {
timeDetected = "2018-05-28T16:00:43Z";
}
任何帮助将不胜感激,因为我已经在这个简单问题上停留了一段时间,并且没有取得太大进展。
答案 0 :(得分:1)
我假设您正在使用observeSingleEvent(of:.value读取父节点,所以此函数读取数据,保持顺序,安全地展开每个子节点并打印时间。我将该值转换为String,但是可以使用double等等。我建议使用double来存储您的时间戳。
func readTimes() {
let ref = self.ref.child("00001")
ref.observeSingleEvent(of: .value, with: { snapshot in
let allChildren = snapshot.children.allObjects as! [DataSnapshot]
for snap in allChildren {
if let time = snap.childSnapshot(forPath: "timeDetected").value as? String {
print("timeDetected = \(time)")
}
}
})
}
和输出
timeDetected = 2018-05-28T16:00:13Z
timeDetected = 2018-05-28T16:00:18Z
timeDetected = 2018-05-28T16:00:43Z
答案 1 :(得分:0)
for item in snapshot.children {
if let itemSnapshot = item as? Snapshot {
for timeDetected in itemSnapshot.children {
print(timeDetected)
}
}
}