我在Swift中使用Firebase是新手,我看到同一条船上有更多人。我的想法是检索每个球员的得分。 Firebase db是这样的:
哪个是根引用(dbRef = FIRDatabase.database().reference()
)
我的代码是这样做的:
dbRef.queryOrdered(byChild: "score").observeSingleEvent(of: .value) { (snap: FIRDataSnapshot) in
for child in snap.children{
print(child)
}
}
在终端中生成这些消息:
Snap (zfUNewjOCsQ5VCtSLFXSXLSFXtJ3) {
addedByUser = "julio@gmail.com";
score = 100;
}
Snap (yfJqO65qCwTTlgzrcw1ovmPgZ6l2) {
addedByUser = "oswaldo@gmail.com";
score = 1000;
}
如何获得分数,以便在终端中打印的信息如下:
100
1000
提前致谢
答案 0 :(得分:1)
您使用childSnapshotForPath
来获得分数:
dbRef.queryOrdered(byChild: "score").observeSingleEvent(of: .value) { (snap: FIRDataSnapshot) in
for child in snap.children{
print((child as AnyObject).childSnapshot(forPath: "score").value)
}
}