gpt4 book ai didi

ios - Firebase 如何获取 NSDictionary 数据

转载 作者:行者123 更新时间:2023-11-29 00:15:34 25 4
gpt4 key购买 nike

我想在用户配置文件中获取一些数据,我有一些信息,但我被困在了这一点上。

Firebase 数据库:enter image description here

这是我的一些代码:

self.ref.child("users").child("profile").observe(.childAdded, with: { (snapshot) in


if let dictionary = snapshot.value as? [String:Any] {

let user = User()

let brands = dictionary["status"] as! NSDictionary


user.displayname = dictionary["displayname"] as? String
user.isconnected = brands["isconnected"] as? String

print(user.isconnected) //fatal error: unexpectedly found nil while unwrapping an Optional value


class User: NSObject {
var displayname: String?
var isconnected: String?
}

最佳答案

尝试这样的事情:

guard let dictionary = snapshot.value as? [String: Any] else {
return
}

guard let statusDictionary = dictionary["status"] as? [String: Any] else {
return
}

guard let deviceStatusDictionary = statusDictionary["DEVICE_KEY"] as? [String: Any] else {
return
}

let user = User()

user.displayname = dictionary["displayname"] as? String
user.isconnected = deviceStatusDictionary["isconnected"] as? Bool

您需要知道您的设备 key 。

和:

class User: NSObject {
var displayname: String?
var isconnected: Bool?
}

关于ios - Firebase 如何获取 NSDictionary 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45371342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com