How to convert a floating point value greater than Int.max to Int in Swift
Int64
is large enough to hold a time interval of some million
years measured in milliseconds:
let milliSeconds = Int64(someDate.timeIntervalSince1970 * 1000)
let milliSecondsString = String(milliSeconds)
The crucial part is to use Int64
instead of Int
on 32-bit platforms. It will allow you to use 8 bytes of memory (from −9223372036854775808 to +9223372036854775807)
Int64(NSDate().timeIntervalSince1970 * 1000)
Int64 is enough to hold the value you needed
let floatingPointValue = NSDate().timeIntervalSince1970 * 1000
let intValue = Int64(floatingPointValue)