2016-10-17 66 views
0

我的火力點設置節點的孩子是這樣:如何提取數據快照

Parent_node:{ 
    Type:{ 
     1476663471800:{ //This is a timestamp = Int64(date.timeIntervalSince1970 * 1000.0) 
      uid: USERS_UID; 
     } 
    } 
} 

我怎麼會訪問用戶的uid?我曾嘗試下面的代碼,但它不是提取UID

self.databaseRef.child("Parent_node/\(Type)").queryLimitedToLast(5).observeEventType(.Value, withBlock: { (snapshot) in 

    print(snapshot) 
    if let userDict = snapshot.value as? [String:AnyObject]{ 
     for each in userDict{ 
      let uidExtraced = each 
      print(uidExtraced) 
      //("1476663471700", [uid: USERS_UID]) 
+0

你想'USERS_UID'? ??? –

+0

你好!是的,我試圖提取USERS_ID :( – gk103

+0

我收到一個錯誤:類型'(String,AnyObject)'沒有下標成員 – gk103

回答

3

首先都使用snapshot.value?.allValues得到的值比解析它...

if snapshot.exists() { 
    for value in (snapshot.value?.allValues)!{ 
      print(value) // you get [uid: USERS_UID] here 

      // ... parse it to get USERS_UID 
      print("user_id -- \(value["uid"])") 
     } 
    } 

With this method, order of child might be different. For ordered nodes, you can use snapshot.child

+0

太棒了!非常感謝你,這工作 – gk103

+0

@ gk103高興它的作品:) –