好的,我正在接收Firebase数据库中的快照,这是我的代码以及我收到的内容:
rootRef.child("Battles").queryOrderedByChild("Player1").queryEqualToValue(CurrentFBID).observeSingleEventOfType(.Value, withBlock: { snap in
if (snap.value is NSNull) {
print("none")
} else {
print(snap.value)
}
})
Recieving:
{
" 04361D33-AC99-4795-BB9A-0B0879882251" =
{
BattleUUID =" 04361D33-AC99-4795-BB9A-0B0879882251&#34 ;;
Player1 = 10210238266903604;
Player2 = 12121212121212;
得分=" 0-0&#34 ;;
转= 1;
};
}
我的JSON树:
{
"Battles" : {
"04361D33-AC99-4795-BB9A-0B0879882251" : {
"BattleUUID" : "04361D33-AC99-4795-BB9A-0B0879882251",
"Player1" : "10210238266903604",
"Player2" : "12121212121212",
"Score" : "0-0",
"Turn" : "1"
},
"07261D43-GC59-4795-BB9A-0B0871635751" : {
"BattleUUID" : "04361D33-AC99-4795-BB9A-0B0879882251",
"Player1" : "1342536342535",
"Player2" : "1524343564",
"Score" : "3-1",
"Turn" : "5"
}
}
}
我如何拆分这些数据作为例子:
let player1 = snap.Player1.value
let player2 = snap.Player2.value
let battleUUID = snap.BattleUUID.value
let score = snap.Score.value
let turn = snap.Turn.value
答案 0 :(得分:0)
您正在接收所有数据。 snap
包含children
,每个child
属于特定的战斗。
要获取与每个孩子相对应的数据,您必须迭代children
snap
作为:{/ p>
for item in snap.children {
let player1 = item.value?["Player1"] as! String
let player2 = item.value?["Player2"] as! String
let battleUUID = item.value?["BattleUUID"] as! String
let score = item.value?["Score"] as! String
let turn = item.value?["Turn"] as! String
}