0

If I am correct: EXC_BAD_INSTRUCTION means that the app crashed because something does not exists. (After googling it and trying to get my head round it)

However I am getting the error:

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

When I tried and load another view.

This is the function where the app crashes:

 func loginUserNeedsEmailVerification() {

    let next = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
    next.isUserLoggedInButNeedsEmail = true
    self.present(next, animated: true, completion: nil)
}

I am calling this function. From another function like so:

perform(#selector(loginUserNeedsEmailVerification), with: nil, afterDelay: 0)

Even though the file exists, I changed the ! to ? like so:

let next = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
    next?.isUserLoggedInButNeedsEmail = true
    self.present(next?, animated: true, completion: nil)

Then another error thrown at the last line:

Value of optional type is not unwrapped.

When I click the little fix it. It changed the code to this:

self.present((next?)!, animated: true, completion: nil)

But then that also errors like so:

optional chain has no effect

What am I doing wrong, and how do I fix this?

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
JamesG
  • 1,552
  • 8
  • 39
  • 86
  • Are you declaring a global variable with database reference in any of your classes? – Dravidian Nov 21 '16 at 19:18
  • @Dravidian i'm not sure what that has to do with a view controller being presented? and yes I have the standard FIRDatabase.database().reference() – JamesG Nov 21 '16 at 23:33
  • @JamesG this crash has nothing to do with Firebase. Check whether the `storyboard` property is `nil` or the `UIViewController` instance returned by the `instantiateViewController` method is `nil` – Luca D'Alberti Mar 07 '17 at 07:37

1 Answers1

1

I just came across the same issue during instantiateViewController, but it's a bit difference since I am calling the method from another class.

A common way to see what goes wrong is by setting the ? as ! after storyboard like this : self.storyboard!.instantiateViewController

You will likely see this error
fatal error: unexpectedly found nil while unwrapping an Optional value,
which means that the storyboard is found nil. In my case, I somehow fail to get the existing reference of UIStoryboard (since I called from another class).

The solution I found is to re-declare the UIStoryboard first:
let next = UIStoryboard(name: "Main", bundle:nil).instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
I am not sure why this work, because it still does not get a current reference of storyboard, but it works for me.

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
Yoho
  • 107
  • 1
  • 13