what does Error “Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)” mean?

后端 未结 10 992
情歌与酒
情歌与酒 2020-11-29 02:42

I got this error:

Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

How can I solve this? The code works normally,

相关标签:
10条回答
  • 2020-11-29 03:05

    In my case this was caused by an integer overflow. I had a UInt16, and was doubling the value to put into an Int. The faulty code was

    let result = Int(myUInt16 * 2)
    

    However, this multiplies as a UInt16, then converts to Int. So if myUInt16 contains a value over 32767 then an overflow occurs.

    All was well once I corrected the code to

    let result = Int(myUint16) * 2
    
    0 讨论(0)
  • 2020-11-29 03:09

    Your secondNumber seems to be an ivar, so you have to use a local var to unwrap the optional. And careful. You don't test secondNumber for 0, which can lead into a division by zero. Technically you need another case to handle an impossible operation. For instance checkin if the number is 0 and do nothing in that case would at least not crash.

    @IBAction func equals(sender: AnyObject) {
    
        guard let number = Screen.text?.toInt(), number > 0 else {
            return
        }
    
        secondNumber = number
    
        if operation == "+"{
            result = firstNumber + secondNumber
        }
        else if operation == "-" {
            result = firstNumber - secondNumber
        }
        else if operation == "x" {
            result = firstNumber * secondNumber
        }
        else {
            result = firstNumber / secondNumber
        }
        Screen.text = "\(result)"
    }
    
    0 讨论(0)
  • 2020-11-29 03:09

    I got this on WatchOS Sim. The issue persisted even after:

    • Deleting the app
    • Restarting Xcode
    • Deleting Derived Data

    ...and was finally fixed by "Erase all Content and Settings" in the simulator.

    0 讨论(0)
  • 2020-11-29 03:15

    Mine was about

    dispatch_group_leave(group)

    was inside if closure in block. I just moved it out of closure.

    0 讨论(0)
  • 2020-11-29 03:21

    If someone else have got a similar error message, you probably built the app with optimisations(production) flag on and that's why the error message is not that communicative. Another possibility that you have got the error under development (from i386 I know you were using simulator). If that is the case, change the build environment to development and try to reproduce the situation to get a more specific error message.

    0 讨论(0)
  • 2020-11-29 03:22

    mine was DispatchQueue.main.sync inside the closer I made it DispatchQueue.main.async and it worked.

    0 讨论(0)
提交回复
热议问题