iOS swift5.0 NSNumber 转 Sting 遇到的问题(Swift中NSNumber转String)

编译器报错:Could not cast value of type ‘__NSCFNumber’ (0x…) to ‘NSString’ (0x…).

解决方式:
The value is an NSNumber, not an NSString. You can use stringValue to convert it:

if let a = d["a"] as? NSNumber {
    let aString = a.stringValue
    println(aString) // -1
}

If you’re sure it’s there, you can use forced unwrapping and string interpolation:

let a = d["a"]! as! NSNumber
let aString = "\(a)"

这样子也是直接nsnumber转nsstring了

2.另外的解决方式:

let num: Double = 123456789
let formattor = NumberFormatter()
formattor.numberStyle = .decimal
let str = formattor.string(from: NSNumber(value: num))
print(str)

要用到NumberFormatter这个类,相关属性如下:

open var numberStyle: NumberFormatter.Style

其他问题可以佳窝魏鑫互相学习iOS技术探讨:lixiaowu1129 一起对iOS技术更深入学习

你可能感兴趣的:(iOS,nsdictionry,iOS混淆方案,flutter,swift,ios,objective-c,iOS教程)