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

shim picture shim · Nov 29, 2014 · Viewed 21.3k times · Source

I am loading in lines from a text file with very large numbers. String has the toInt method, but how do you convert a string to an Int64 which will be able to handle the large numbers?

I don't see a toInt64 method or a toLong etc. There must be a way, but I haven't found anything by searching yet.

Alternatively I guess I could read the numbers in the string digit by digit (or by groups of digits) and then add them together with appropriate factors of ten, but that seems like overkill. Or maybe the proper way is to read the data in a binary form and convert it that way?

Thanks

Answer

Laevand picture Laevand · Mar 30, 2016

As of Swift 2.1.1 you can simply initialize Int64 with String

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