EXC_BAD_INSTRUCTION error for doneBarButton

After I compile the app after adding the ListDetailViewControllerDelegate on page 170, the app crashes when I try to add a new item to the Checklists page.

The error I get is on the ListDetailViewController.swift page and in this method:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string:String) -> Bool {
        let oldText = textField.text! as NSString
        let newText = oldText.replacingCharacters(in: range, with: string) as NSString
        doneBarButton.isEnabled = (newText.length > 0)
        return true

The specific line which causes the EXC_BAD_INSTRUCTION error is:

doneBarButton.isEnabled = (newText.length > 0)

The app compiles fine, but when I try to add an item in the Edit page, the app crashes with this error message. Have rebuilt the app twice, and this issue has come back both times.

In the variables window, there is this message:

doneBarButton = (UIBarButtonItem!)nil

I don’t understand how the doneBarButton can be nil?

Thanks in advance.

How is the UIBarButtonItem declared in the class?

It sounds like you did not connect the doneBarButton outlet to the bar button in the storyboard.

Thank you, yes that was my problem.

I was too focused on the code and forgot to check my connections!