我有一个来自Cloud Firestore的数字字段,需要在标签中显示为字符串。
通常,如果字段是字符串,我可以执行以下代码
db.collection("users").document(uid ?? "UID not yet loaded in viewDidLoad()")
.addSnapshotListener { snapshot, error in
if error != nil {
print(error ?? "Couldn't update text field TextUser according to database")
} else {
if let dbUsername = snapshot?["username"] as? String {
self.textUser?.text = dbUsername
}
这是因为文档中的“username”是一个值字符串。
但这不起作用,因为文件中的“现金”是一个有价值的数字。
if let dbCash = snapshot? ["cash"] as? String {
self.labeCash?.text = dbCash
}
我可能只需要把数字,不管它们使用什么类型,转换成一个字符串。但我该怎么做呢?谢谢!
最佳答案
你能试试吗
if let dbCash = snapshot? ["cash"] as? NSNumber {
self.labeCash?.text = dbCash.stringValue
}
关于ios - Cloud Firestore Swift:如何在标签中显示数字字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51866991/