2016-03-06 38 views
1

我想從當前數據獲取下一個KEY。 所以我有:如何從Firebase獲取Swift中的下一個數據

{ 
    "Key1":"Value1", 
    "Key2":"Value2", 
    "Key3":"Value3"    
    } 

而且我可以用這個代碼得到它:

let historyUrl = firebase.childByAppendingPath("Data") 

historyUrl.observeEventType(.Value, withBlock: { 
      historyValue in 
      print(historyValue.value) 
     }) 

如何在密鑰1,密鑰2,密鑰,得到不同的值時,我不知道KEY的名稱,並不解碼json?

回答

2

你總是可以得到一個快照

的值的鍵和值:

print(historyValue.value) 

關鍵:

print(historyValue.key) 

使用.value的時候,它會在整個閱讀父節點的內容。任何時間值對

for child in historyValue.children { 
    print(child.key) 
    print(child.value) 
} 

另一種選擇是使用.childAdded,這一次會遍歷每個人的孩子之一,然後再一個新的子:您可以通過快照,然後重複,以獲得單獨密鑰被添加。

ref.observeEventType(.ChildAdded, withBlock: { historyValue in 
    println(historyValue.key) 
    println(historyValue.value) 
}) 
相關問題