what does Error "Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)" mean?
IosSwiftExceptionRuntime ErrorIos Problem Overview
I got this error:
>Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
How can I solve this? The code works normally, but in the calculator when I click the only equal button, it gives that error.
@IBAction func equals(sender: AnyObject) {
secondNumber = Screen.text!.toInt()! // here it shows an error which is "Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)"
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)"
}
Ios Solutions
Solution 1 - Ios
This line
secondNumber = Screen.text!.toInt()!
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.
That's what the !
means: "I am sure this thing exists, so please crash if it doesn't". And crash is what it did.
Solution 2 - Ios
Generally, EXC_BAD_INSTRUCTION means that there was an assertion failure in your code. A wild guess, your Screen.text
is not an integer. Double check its type.
Solution 3 - Ios
Mine was about
> dispatch_group_leave(group)
was inside if closure in block. I just moved it out of closure.
Solution 4 - Ios
mine was DispatchQueue.main.sync inside the closer I made it DispatchQueue.main.async and it worked.
Solution 5 - Ios
I got this error while I tried to write to a variable at the same time from different threads. Creating a private queue and making sure one thread at a time can write to that variabele at the same time. It was a dictionary in my case.
Solution 6 - Ios
In my case it happened when 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, and that caused this error.
I have solved the issue by checking if the managed object exists or not before calling the function.
Solution 7 - Ios
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
Solution 8 - Ios
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)"
}
Solution 9 - Ios
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.
Solution 10 - Ios
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.
Solution 11 - Ios
try to clear workspace.
rm -rf ' ~/Library/Application\ Support/"your programm name" '