在Swift中从Firebase获取快照密钥
以下是我的Firebase数据库的外观: 我想得到的名字是“攻击尼贝瓦”,“卡普佐堡”等等,而不是其他的东西。我当前的代码提供了完整的JSON:在Swift中从Firebase获取快照密钥,swift,firebase,firebase-realtime-database,Swift,Firebase,Firebase Realtime Database,以下是我的Firebase数据库的外观: 我想得到的名字是“攻击尼贝瓦”,“卡普佐堡”等等,而不是其他的东西。我当前的代码提供了完整的JSON: let ref = Database.database().reference().child("Battle Details") ref.observeSingleEvent(of: .value, with: { (snap : DataSnapshot) in print("\(String(describi
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检索名称。按
键检索名称,请尝试以下操作:
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