ios - 为什么此代码在 iPhone 5 上崩溃?

标签 ios iphone swift crash 32-bit

以下代码:

func getCurrentMillis() -> Int64 {
    return Int64(Date().timeIntervalSince1970 * 1000)
}

在 [32 位] iPhone 5 上崩溃并显示消息:

EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xe7ffdefe)

我不明白为什么这个计算的结果似乎应该适合 Int64,还是我遗漏了什么?

Stacktrace 显示此(TextProcessor.textDidChange() 调用 getCurrentMillis()):

enter image description here

应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.StrideInt,所以当timeOfLastInput0时,运算结果溢出在 32 位系统中。

要解决 Swift 的这种行为,您可以将行更改为:

if getCurrentMillis() > timeOfLastInput + 150 {

无论如何,你最好发一个bug report to Appleto swift.org .

关于ios - 为什么此代码在 iPhone 5 上崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42133129/

相关文章:

ios - 在objective c中实现RC4加密和URL编码

swift - 有人可以解释结构与枚举中定义的常量的内存分配吗?

ios - 在 UICollectionview iOS 8 中包装单元格

ios - 在 iOS 上使用 OpenGLES 进行多图协作

iphone - Xcode 单元测试

ios - 在 Objective-C 中查找数组中的元素子数组的最有效方法是什么?

ios - 在 iO 中使用 Open GL 1.0 模板缓冲区进行屏蔽的问题

iphone - 有人在 Snow Leopard 上进行 iPhone SDK 2.x.x 开发吗?

ios - 如何访问在 xib 中创建的自定义单元格

ios - 自定义可选类在分配给 nil 时不释放