Swift: Cast Any Object to Int64 = nil

a 夏天 提交于 2019-12-01 18:59:04

In

let dict : [String : AnyObject] = ["intValue": 1234, "stringValue" : "some text"]

the number 1234 is stored as an NSNumber object, and that can be cast to Int, UInt, Float, ..., but not to the fixed size integer types like Int64.

To retrieve a 64-bit value even on 32-bit platforms, you have to go via NSNumber explicitly:

if let val = dict["intValue"] as? NSNumber {
    let int64value = val.longLongValue // This is an `Int64`
    print(int64value)
}

You can now directly use val.int64value instead of val.longLongValue, provided val is a NSNumber

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!