xcode - 无法将 nsnumber 类型的值分配给值类型字符串

标签 xcode swift nsnumberformatter

我正在使用数字格式化程序将值数字从 JSON 更改为带逗号的数字(例如 500000500,000)。我想我已经记下了,但我收到一条错误消息:

cannot assign a value of type nsnumber to a value type string

if let supply = json["supply"] as? NSNumber{
    var numberFormatter = NSNumberFormatter()
    numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
    numberFormatter.stringFromNumber(supply)!
    totalXRP.text = supply

}

最佳答案

您必须将 numberFormatter.stringFromNumber 的结果分配给您的文本字段:

if let supply = json["supply"] as? NSNumber{
    var numberFormatter = NSNumberFormatter()
    numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
    if let result = numberFormatter.stringFromNumber(supply) {
        totalXRP.text = result
    }
}

关于xcode - 无法将 nsnumber 类型的值分配给值类型字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31023351/

相关文章:

ios - 如何在 Swift 中从 "without navigation bar view controller"转到 "with navigation bar view controller"

ios - 从 UTC-5 时区日期和时间 iPhone 应用程序获取当前 iPhone 设备时区日期和时间?

c++ - 字符串以某个字符串开头

ios - WatchKit 并发症 : get Complication data from extension delegate

ios - 使用 UIPinchGestureRecognizer 调整 UIView 的大小

ios - NSNumberFormatter SpellOutStyle 为 "and"

ios - 如何设置 NSNumberFormatter 以使用 "万"(日文/中文 10,000 标记)显示数字?

objective-c - 是否有可用于 double 的字符串格式化程序?

xcode - Gitlab - Xcode 无法连接远程存储库

ios - 有没有办法在 iOS 应用程序更新期间运行代码?