In Swift, how do you convert a String to Int64? In Swift, how do you convert a String to Int64? xcode xcode

In Swift, how do you convert a String to Int64?


As of Swift 2.1.1 you can simply initialize Int64 with String

let number: Int64? = Int64("42")


If you don't mind using NSString, you can do this

let str = "\(LLONG_MAX)"let strAsNSString = str as NSStringlet value = strAsNSString.longLongValue

Note that unlike toInt(), longLongValue will return 0 if the string is not a legal number whereas toInt() will return nil in that case.


import Darwinlet strBase10 = "9223372036854775807"let i64FromBase10 = strtoll(strBase10, nil, 10)let strBase16 = "0xFFFFFFFFFFFFFFFF"let i64FromBase16 = strtoll(strBase16, nil, 16)

strtoll means STRingTOLongLong

http://www.freebsd.org/cgi/man.cgi?query=strtoll