我有一个 FB 实时数据库,我正在查询其中的一部分。
let databaseRef = Database.database().reference(withPath: "developer")
databaseRef.observe(.value) { (snapshot) in
snapshot.children.forEach({ (child) in
print(child)
})
}
这将打印以下内容:
Snap (0) {
email = "foo@bar.com";
id = 1;
image = "dolecki.png";
name = "Mr. Dolecki";
}
Snap (1) {
email = "foo@bar.com";
id = 2;
image = "bourque.png";
name = "Mr. Bourque";
}
Snap (2) {
email = "foo@bar.com";
id = 3;
image = "shaw.png";
name = "Mr. Shaw";
}
如何从这些内容中提取我想要的部分(例如每个名称、ID 等)?我无法转换为字典,因为我收到一条错误,指出它作为数组出现。
最佳答案
我想我现在已经拥有了。
let databaseRef = Database.database().reference(withPath: "developer")
databaseRef.observe(.value) { (snapshot) in
print(snapshot.childrenCount) //3
if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
for snap in snapshot {
if let postDict = snap.value as? Dictionary<String, AnyObject> {
if let name = postDict["name"] {
print(name)
}
//print(postDict.keys) // name, email, id, image
} else {
print("Failed to convert.")
}
}
}
}
关于swift - Firebase Realtime DB - 从数组获取特定数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56873409/