以下代码:
func getCurrentMillis() -> Int64 {
return Int64(Date().timeIntervalSince1970 * 1000)
}
在 [32 位] iPhone 5 上崩溃并显示消息:
EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xe7ffdefe)
我不明白为什么这个计算的结果似乎应该适合 Int64
,还是我遗漏了什么?
Stacktrace 显示此(TextProcessor.textDidChange()
调用 getCurrentMillis()
):
应OOPer的要求,我添加了TextProcessor的相关代码:
var timeOfLastInput: Int64 = 0
...
if getCurrentMillis() - timeOfLastInput > 150 {
textMap.cursorPosition = nil
}
更新: 我已将错误报告发送给 Apple。
最佳答案
我不确定 Swift 编译器中发生了什么,但是当您编写时:
if getCurrentMillis() - timeOfLastInput > 150 {
Swift 在许多重载的减法运算符中使用了这个:
public func -<T : Strideable>(lhs: T, rhs: T) -> T.Stride
(使用 Xcode 8.2.1 测试。)
不幸的是,Int64.Stride
是Int
,所以当timeOfLastInput
为0
时,运算结果溢出在 32 位系统中。
要解决 Swift 的这种行为,您可以将行更改为:
if getCurrentMillis() > timeOfLastInput + 150 {
无论如何,你最好发一个bug report to Apple或 to swift.org .
关于ios - 为什么此代码在 iPhone 5 上崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42133129/