gpt4 book ai didi

ios - Swift 中的 Firebase。无法将类型 'NSNull' 的值转换为 'NSDictionary'

转载 作者:搜寻专家 更新时间:2023-11-01 07:16:50 25 4
gpt4 key购买 nike

每次打开我的应用程序并连接时,都会导致错误。我认为我将值正确地转换为 [String: String] 但不是。解决这个问题的正确方法是什么?

 class func info(forUserID: String, completion: @escaping (User) -> Swift.Void) {
FIRDatabase.database().reference().child("users").child(forUserID).child("credentials").observeSingleEvent(of: .value, with: { (snapshot) in

//This line is the reason of the problem.
let data = snapshot.value as! [String: String]
let name = data["name"]!
let email = data["email"]!
let link = URL.init(string: data["profile"]!)
URLSession.shared.dataTask(with: link!, completionHandler: { (data, response, error) in
if error == nil {
let profilePic = UIImage.init(data: data!)
let user = User.init(name: name, email: email, id: forUserID, profilePic: profilePic!)
completion(user)
}
}).resume()
})
}

错误说

Could not cast value of type 'NSNull' (0x1ae148588) to 'NSDictionary' (0x1ae148128).

最佳答案

当网络服务返回值 <null> 时, 它被表示为 NSNull目的。那是一个实际的对象并将其与 nil 进行比较将返回 false .

这是我的做法:

if let json = snapshot.value as? [String: String] {
//if it's possible to cast snapshot.value to type [String: String]
//this will execute
}

关于ios - Swift 中的 Firebase。无法将类型 'NSNull' 的值转换为 'NSDictionary',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41744498/

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