ios - 我想念在 firebase 中解析 JSON 的任何内容吗

标签 ios json swift firebase firebase-realtime-database

我花了很多时间想不通。为什么为什么不解析。 enter image description here

func fetchUser(_ uid: String) {
    FIRDatabase.database().reference().child("users").child("g373gSvDcFhXNAGh5V6vg7sU5C02").observe(.childAdded, with: {
        (snapshot) in
        //TODO...

        print(snapshot) //Snap (email) test1@gmail.com
        let user = snapshot.value as ? [String: Any]

        print(user) //nil

        if let _nameLabel = self.nameLabel,
            let name = user ? ["name"] as ? String {
                _nameLabel.text = "{-} \(name)"
            }

    }, withCancel: nil)

}

最佳答案

问题:

您使用事件类型 .childAdded 进行观察

case childAdded is fired when a new child node is added to a location

解决方案:

您应该将事件类型从 .childAdded 更改为 .value,如下所示:

FIRDatabase.database().reference().child("users").child("g373gSvDcFhXNAGh5V6vg7sU5C02").observe(.value, with: { (snapshot) in

  // other code

})

关于ios - 我想念在 firebase 中解析 JSON 的任何内容吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41784134/

相关文章:

ios - Build 1.0.0 不包含正确的测试版权利。有关详细信息,请参阅 iTunes Connect 开发人员指南

ios - 如何在 alamofire 中快速打印错误 html?

swift - 从服务器异步下载信息

ios - 当其他 UILabel 增长时自动缩小 UILabel

ios - 如何存储[[string]]并从核心数据中获取它?

android - 使用电话设备时,我在 ionic3 中看不到 JSON 数据(来自外部 Url 的 JSON 数据)

java - 如何使用从 Spring MVC 发回的 JSON 对象填充 jQuery 数据表的行?

node.js - 无法使用 slackbots (javascript) 通过 Slackbot 发送图像

ios - 减小 SpriteKit 节点的大小

ios - 漏洞 : `UITextField.appearance().tintColor` changes `rightBarButtonItem` color