这是我的 Firebase 数据库的样子:
我想获取名称“进击 Nibeiwa”、“Fort Capuzzo”等,而不是其他内容。我当前的代码提供了完整的 JSON:
let ref = Database.database().reference().child("Battle Details")
ref.observeSingleEvent(of: .value, with: { (snap : DataSnapshot) in
print("\(String(describing: snap.value))")
}) { (err: Error) in
print("\(err.localizedDescription)")
}
最佳答案
要检索名称,请尝试以下操作:
let ref = Database.database().reference().child("Battle Details")
ref.observeSingleEvent(of: .value, with: { (snap : DataSnapshot) in
for child in snap.children {
let key = (child as AnyObject).key as String
}
}) { (err: Error) in
print("\(err.localizedDescription)")
}
这里的快照位于 Battle Details
,然后您在直接子代中迭代,在这种情况下是名称,并使用 child.key
关于swift - 在 Swift 中从 Firebase 获取快照 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51306132/