如何从数据快照中提取节点的子节点 [英] How to extract child of node in data snapshot

查看:91
本文介绍了如何从数据快照中提取节点的子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的firebase设置是这样的:

  Parent_node:{
类型:{
1476663471800 :{//这是一个时间戳= Int64(date.timeIntervalSince1970 * 1000.0)
uid:USERS_UID;



code $
$ b我将如何访问用户的uid?我已经尝试了下面的代码,但它不提取UID

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

print(snapshot)
if userDict = snapshot.value as?[String:AnyObject ] {
for userDict {
let uidExtraced = each
print(uidExtraced)
//(1476663471700,[uid:USERS_UID])
<首先使用 snapshot.value?.allValues


解决方案

来获取值并解析它...
$ b $ pre $如果snapshot.exists(){
(value.value?.allValues)!{
print(value)//你在这里获得[uid:USERS_UID]

// ...解析它得到USERS_UID
print(user_id - \(value [uid]))
}
}




有了这个方法,孩子的顺序可能会有所不同。对于有序节点,您可以使用 snapshot.child



My firebase set up is as such:

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

how would I access the users uid? I have tried the following code, but its not extracting the 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])

解决方案

First of all use snapshot.value?.allValues to get values and than parse it...

  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

这篇关于如何从数据快照中提取节点的子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆