ios - Cloud Firestore Swift:如何在标签中显示数字字段
问题描述
我有一个来自 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
}
之所以有效,是因为文档中的“用户名”是一个值字符串。但这不起作用,因为文档中的“现金”是一个价值数字。
if let dbCash = snapshot? ["cash"] as? String {
self.labeCash?.text = dbCash
}
我可能只需要将数字(无论他们使用什么类型)转换为字符串。但我该怎么做呢?谢谢!
解决方案
你能试一下吗
if let dbCash = snapshot? ["cash"] as? NSNumber {
self.labeCash?.text = dbCash.stringValue
}
推荐阅读
- wordpress - 通过 2 个单独的 Wordpress 安装,如何实现一个新成员在一个站点上注册并自动插入另一个站点?
- angular - 如何在 Angular 组件中有一个变量 h1..h6 元素?
- javascript - 将立方体的一个侧面粘贴到另一侧时遇到问题
- ios - 使用 TabViewController,保存数据时,TableView 不更新
- python - OpenCV中的图像阈值
- reactjs - 授权 Laravel 中的 ERR_EMPTY_RESPONSE 错误
- uwp - UWP PrintPageRangeOptions.AllowCustomSetOfPages 属性不起作用
- python - ImportError:无法从“teacher.models”导入名称“GuideTeacher”
- javascript - 在 Promise 中的 Angular 8 HttpClient 请求中失败,并且承诺永远不会解决
- artifactory - Artifactory:java.io.IOException:无法部署文件。状态码:404 响应信息