70

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)"
}
iamVishal16
  • 1,780
  • 18
  • 40
legolas
  • 725
  • 1
  • 5
  • 7
  • In this line `result = firstNumber / secondNumber` what happens when `secondNumber` equals zero? – Jonny Henly Mar 02 '15 at 07:50
  • yeah i got it that is undefined, so how can i fix it? – legolas Mar 02 '15 at 07:53
  • "Dividing a number by zero (`i / 0`), or trying to calculate remainder by zero (`i % 0`), causes an error." - [The Swift Programming Language - Advanced Operators](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html) – Jonny Henly Mar 02 '15 at 07:55

14 Answers14

135

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.

pkamb
  • 33,281
  • 23
  • 160
  • 191
gnasher729
  • 51,477
  • 5
  • 75
  • 98
14

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.

Ashraf Tawfeeq
  • 3,036
  • 1
  • 20
  • 34
10

Mine was about

dispatch_group_leave(group)

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

alicanbatur
  • 2,172
  • 1
  • 28
  • 36
  • I think i have the same issue and I am aware that this was in '17. Here is my code. If you could help me out what you did exactly, that would really be great: if(aGroup == nil) { block(); } else { dispatch_async(serialQueue, ^{ dispatch_group_enter(aGroup); block(); dispatch_group_leave(aGroup); }); } By closure =, do you mean the block? So I simply put it after }); right? – Harshal Karande Oct 22 '21 at 18:36
6

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

pankaj nigam
  • 381
  • 1
  • 6
  • 9
4

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.

J. Doe
  • 12,159
  • 9
  • 60
  • 114
4

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.

Luke
  • 965
  • 8
  • 21
3

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
Kiwi
  • 109
  • 8
  • Thanks so much! This save me a lot of time! I was trying to compute the average of a list of UInt8 value. When I was computing the sum, I got an overflow! – J.beenie Jan 30 '20 at 07:19
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)"
}
Helge Becker
  • 3,219
  • 1
  • 20
  • 33
0

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

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.

Andres Canella
  • 3,706
  • 1
  • 35
  • 47
0

In my case it was because I was trying to start (NS)Operation by directly using its start() function. But instead what I needed to do is to pass it to OperationQueue. Something like this var queue = OperationQueue(); queue.addOperation(myOperation)

mykolaj
  • 974
  • 8
  • 17
0
  • In my case, I am reusing the tableview_cell class in 2 different controller classes,

  • and I added a new outlet to one of the cell but it's not available for the other controller cell class, because of this it showed a connection in class, not able to find it's broken for other class, so I missed the connection for the other class.

  • need to add a connection for that outlet.

iOS_Tejas
  • 209
  • 2
  • 9
0

EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

You are able to run into this error when you use Core Data Concurrency Debugging on launch Arguments. App throws this error when you access a managed object on the wrong queue[About]

-com.apple.CoreData.ConcurrencyDebug 1
yoAlex5
  • 29,217
  • 8
  • 193
  • 205
-2

try to clear workspace.

rm -rf ' ~/Library/Application\ Support/"your programm name" '
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Vova Denys
  • 32
  • 2