EXC_BAD_INSTRUCTION only in iPhone 5 simulator

限于喜欢 提交于 2019-12-02 13:32:01

问题


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?


回答1:


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))


来源:https://stackoverflow.com/questions/40811559/exc-bad-instruction-only-in-iphone-5-simulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!