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)"
}
This question is related to
ios
swift
exception
runtime-error
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.
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
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)"
}
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.
try to clear workspace.
rm -rf ' ~/Library/Application\ Support/"your programm name" '
I got this on WatchOS Sim. The issue persisted even after:
...and was finally fixed by "Erase all Content and Settings" in the simulator.
mine was DispatchQueue.main.sync inside the closer I made it DispatchQueue.main.async and it worked.
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.
Mine was about
dispatch_group_leave(group)
was inside if closure in block. I just moved it out of closure.
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.
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.
Source: Stackoverflow.com