gpt4 book ai didi

ios - 将 Firebase 值(Int)转换为字符串会导致错误

转载 作者:行者123 更新时间:2023-11-28 16:09:01 25 4
gpt4 key购买 nike

我正在尝试从 Firebase 中的 JSON 树中获取一些值,但是出现错误:

Could not cast value of type '__NSCFNumber' (0x1a7c497f0) to 'NSString' (0x1a7c55398).

我设置值的方式是这样的:

let values = ["username": username, "email": email, "reputation": reputation] as [String : Any]

我得到这样的值:

self.databaseRef.child("users").child(self.loggedInUser!.uid).observeSingleEvent(of: .value) { (snapshot:FIRDataSnapshot) in

//store the logged in users details into the variable

print(self.loggedInUser)
let snapshot = snapshot.value as! [String: AnyObject]
self.usernameLabel.text = snapshot["username"] as! String
self.reputationLabel.text = snapshot["reputation"] as! String//This is giving me error
})

我做错了什么?是什么原因造成的?它在 Swift 2 中有效,但在 Swift 3 中无效。

最佳答案

您从 firebase 检索的数据可能是 double 类型。尝试使用这个:

self.reputationLabel.text = "\(snapshot["reputation"])"

编辑:

如果这给你 nil,请尝试使用条件解包:

if let reputation = snapshot["reputation"] {
self.reputationLabel.text = "\(reputation)"
} else {
print("reputation is nil")
}

如果你的数据是 nil 然后检查你在 firebase 中的数据名称,以及你在数据库中的对象

关于ios - 将 Firebase 值(Int)转换为字符串会导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39829106/

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