Dreanaught Dreanaught - 1 year ago 98
Swift Question

EXC_BAD_INSTRUCTION only in iPhone 5 simulator

Running my code on the iPhone 5 simulator throws the exception shown in the image.
Running the code on any of the other simulators is just fine.

I can't spot where I made a mistake in this unspectacular line of code.
Does anyone else have this problem?

Exception shown in XCode

Answer Source

NSInteger (which is a type alias for Int in Swift) is a 32-bit integer on 32-bit platforms like the iPhone 5. The result of

NSInteger(NSDate().timeIntervalSince1970) * 1000

is 1480106653342 (at this moment) and does not fit into the range -2^31 ... 2^31-1 of 32-bit (signed) integers. Therefore Swift aborts the execution. (Swift does not "truncate" the result of integer arithmetic operations as it is done in some other programming languages, unless you specifically use the "overflow" operators like &*.)

You can use Int64 for 64-bit computations on all platforms:

Int64(NSDate().timeIntervalSince1970 * 1000)

In your case, if a string is needed:

let lastLogin = String(Int64(NSDate().timeIntervalSince1970 * 1000))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download