【问题标题】:How can I read the value of a Firebase database dictionnary created using .childByAutoId() in Swift?如何读取在 Swift 中使用 .childByAutoId() 创建的 Firebase 数据库字典的值?
【发布时间】:2018-01-04 20:54:21
【问题描述】:

我有一个存储在 Firebase 数据库中的字符串字典。如下图所示。

如您所见,每个条目都是使用.childByAutoId() 创建的,并包含两个变量:texttag。 我希望能够浏览所有条目,并将text 的值与我的应用程序中本地保存的变量进行比较。我尝试了很多方法,但找不到任何有效的解决方案。我应该如何进行?

提前感谢您的帮助。

【问题讨论】:

  • 显示您现在正在尝试的内容。

标签: swift firebase-realtime-database


【解决方案1】:

您需要在特定参考处观察数据库,然后转换将发送给您的快照。快照代表给定路径的数据库片段

        let dbRef = Database.database().reference().child("messages")
        dbRef.observeSingleEvent(of: .value) { (snapshot) in
            for message in snapshot.children{
                let msg = (message as! DataSnapshot).value //message as snapshot
                //now you need to cast it to your structure([String:String])
                let projectObj = Message(snapshotChild: msg as! [String:String])
               //and do your comparison
            }
        }

【讨论】:

  • 我在使用 Message(snapshotChild: msg as! [String:String]) 时遇到错误,因为 Xcode 无法识别 Message() 方法('使用未解析的标识符'消息')。
  • 当然,Message 只是一个包装类,您可以为自己的情况编写它以使代码更简洁。 print(msg) 看看你在控制台得到了什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-15
相关资源
最近更新 更多