Swift EXC_BREAKPOINT when assigning viewcontroller to variable in prepareForSegue

Im getting an error when trying to perform a variable assignment with my destinationViewController.

The error message is this:
Thread 1: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)

  • Google Analytics SDK 3.0 _sqlite3 linker errors in iOS
  • Generic function taking a type name in Swift
  • How to set a max limit for an IBInspectable Int
  • Delay while loading locally stored file on webView
  • NSUserDefaults, Settings.bundle and App groups
  • Issue with code autocompletion / syntax highlighting in XCode 4.x
  • This in my prepareForSegue function.

    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        if segue.identifier == "LoginSegue"{
    
            let vc = segue.destinationViewController as LoggedInViewController
            vc.email = emailTextfield.text
    
        }
    }
    

    In the other file it looks like this.

    var email: String?
    

    which is at the top.
    Then this:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        println("Email is:")
        println(email)
        println("Email was")
    }
    

    But i never come into the second file.

    It is the line let vc = segue.destinationViewController as LoggedInViewController that is marked with error.

    Both swift files are connected to navigation controllers.

    I dont know what more you need, but I will of course post all code you need to understand!

    Thanks!

    2 Solutions Collect From Internet About “Swift EXC_BREAKPOINT when assigning viewcontroller to variable in prepareForSegue”

    In your case destination controller is navigation controller not your LoggedInViewController , So segue.destinationViewController as LoggedInViewController is an error , therefore it is crashing.

    Try like this

    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        if segue.identifier == "LoginSegue"{
                let navigationController = segue.destinationViewController as UINavigationController
    
            let vc = navigationController.topViewController as LoggedInViewController
            vc.email = emailTextfield.text
    
        }
    }
    

    In case somebody comes here because it’s the first hit on EXC_BREAKPOINT:

    For me this very telling exception was thrown because of a fatal error: unexpectedly found nil while unwrapping an Optional value that happened because an IBOutlet was used before it was initialized.