首页 文章

swift上的EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)

提问于
浏览
3

我在SwiftMoment的这部分代码中遇到了这个问题https://github.com/akosma/SwiftMoment

public func moment(_ timetoken: Int64) -> Moment {
  return moment(Int(timetoken / 10000))
}

我不确定为什么会这样 . 如果您有任何见解,请随时分享 . 谢谢!

enter image description here

这是timetoken值: timetoken 14915504189961350

它发生在Simulator MacOS Sierra 10.12.4上

Xcode 8.3.1 iOS 10.3.1 iPhone 5

更新

这个问题没有出现在iPhone 7上

1 回答

  • 4

    iPhone 5是一个32位设备,这意味着Int is a 32-bit integertimetoken / 10000 的结果不适合 Int . 与其他一些编程语言相比,整数溢出是Swift中的致命运行时错误(这很好,因为否则你会得到错误的结果) .

    我建议将值转换为 TimeInterval (这是一个浮点类型,实际上只是 Double 的类型别名)然后调用

    public func moment(_ seconds: TimeInterval) -> Moment
    

    代替

    public func moment(_ milliseconds: Int) -> Moment
    

相关问题