SMDev SMDev - 10 days ago 5
Swift Question

iOS prepareforsegue throwing xc_bad

I'm using a

prepareForSegue
method,

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if segue.identifier == "roundSegue" {

var selectedRound:NSManagedObject = roundList[self.tableView.indexPathForSelectedRow().row] as NSManagedObject

let SVC: SetupVC = segue.destinationViewController as SetupVC

SVC.course = selectedRound.valueForKey("course") as String
SVC.existingRound = selectedRound
}

}


However, it is throwing an error "Thread 1:EXC_BAD_INSTRUCTION", on the 'if segue.identifier == "roundSegue"' line...any thoughts on why it would be throwing this error?

Answer

It is possible that your segue identifier is nil. If this is the case, you are attempting to unwrap a implicitly unwrapped optional variable that is nil, which would cause a crash. try this instead:

if let identifier = segue.identifier {
    if identifier == "roundSegue" {
        // your code here.
    }
}
Comments