swift NSNumber 转 Sting 遇到的问题

来源:互联网 发布:淘宝互联有手机版吗 编辑:程序博客网 时间:2024/05/21 06:57

编译器报错: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! NSNumberlet aString = "\(a)"

1 0
原创粉丝点击