EXC_BAD_INSTRUCTION only in iPhone 5 simulator

后端 未结 1 1250
野趣味
野趣味 2021-01-23 08:27

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 mad

相关标签:
1条回答
  • 2021-01-23 08:50

    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))
    
    0 讨论(0)
提交回复
热议问题