. Advertisement .
..3..
. Advertisement .
..4..
I encounter the ”thread 1: exc_bad_instruction (code=exc_i386_invop, subcode=0x0)” error when I run my program.
Although the code functions fine, the calculator displays the error when I click the only equal button.
@IBAction func equals(sender: AnyObject) {
secondNumber = Screen.text!.toInt()!
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)"
}
And I receive the error message:
Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
I don’t understand why this error occurs and the way to fix it. Can you help me?
The cause: This error happens because you are calling a function by passing a parameter of a Core Data managed object’s property. At the time of calling the object was no longer existed.
I think this line is a mistake:
It means: Get the Screen object, get the text property and please crash if it doesn’t exist, then get the text converted to an integer, and please crash if it doesn’t exist.
And the
!
means: “I am sure this thing exists, so please crash if it doesn’t”. And it caused this error.Solution: You can fix the issue by checking if the managed object exists or not before calling the function.