EXC_BAD_INSTRUCTION (code = EXC_I386_INVOP, subcode = 0x0) for fast

I ran into this issue in this piece of code using SwiftMoment https://github.com/akosma/SwiftMoment

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

      

I don't know why this is happening. If you have any ideas, feel free to share them. Thank!

enter image description here

here's the timetoken value: timetoken 14915504189961350

This happens on Simulator macOS Sierra 10.12.4

Xcode 8.3.1 iOS 10.3.1 iPhone 5

Update

The problem does not appear on iPhone 7

+3


source to share


1 answer


iPhone 5 is a 32-bit device, which means it Int

is a 32-bit integer
and the result timetoken / 10000

doesn't fit into Int

. Unlike some other programming languages, integer overflow is a fatal runtime error in Swift (which is good, because otherwise you just get the wrong result).

I would suggest converting the value to TimeInterval

instead (which is a floating point type, actually just a type alias for Double

) and then calls

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

      



instead

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

      

+4


source







All Articles