从儿童Firebase访问值 [英] Accessing values from Children Firebase

查看:56
本文介绍了从儿童Firebase访问值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实不太了解Swift 3.0 Firebase语法的更新,但是从快照子级检索值中的大多数似乎都是不可能的.为此,我使用如下代码段:

I am really not quite understanding the updates to the Swift 3.0 Firebase syntax, but most of all retrieving values from children of a snapshot seems to be impossible. To do so, I use a snippet like this:

 if let snapVal = snapshot.value as? [String: AnyObject] {
     for c in snapshot.children {
                        let cx = (c as! AnyObject)
                        let name = cx["NAME"] as! String

     }
}

我已经采取了许多方法,但是FIRDatabaseSnapshot在新的Swift 3更新中有很多限制,并且AnyObject不允许从对象读取值,并且类似NSDictionary的类型也没有孩子.非常感谢所有帮助,谢谢!

I have taken many approaches to this but FIRDatabaseSnapshot has many restrictions in the new Swift 3 update, and AnyObject does not allow for values to be read from the object and NSDictionary-like types do not have children either. All help is very appreciated, thank you!

推荐答案

您收到的类型为FIRDataSnapshot的snapShot实际上是符合 NSObject 的自定义类,因此只有符合以下条件的变量到FIRDataSnapshot可以访问FIRDataSnapshot提供的自定义功能,例如 .children .

The snapShot that you receive of type FIRDataSnapshot is actually a custom class conforming to NSObject, so only the variables conforming to FIRDataSnapshot can access the custom function that FIRDataSnapshot provides such as .children.

但是,当您访问一个值为snap.value解析为 [String:AnyObject] 的新变量时,基本上它成为NSDictionary,而NSDictionary没有任何参数.children.

But when you are accessing a new variable whose value is snap.value parsed as [String:AnyObject], basically it becomes a NSDictionary, and NSDictionary doesn't has any parameter .children.

  FIRDatabase.database().reference().child("your_Ref").observeSingleEvent(of: .value, with: {(snap) in

        for each in snap.children{

            print(each)


        }
      if let snapDict = snap.value as? [String:AnyObject]{

            for each in snapDict{

                let keyID = each.key
                let childValue = each.value["NAME"] as! String
            }
        }

    })

这篇关于从儿童Firebase访问值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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