0
votes

Building the ToDo app. The app crashes when the new todo task is created.

The breakpoint stops the code and returns:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

 @IBAction func doneButton(_ sender: UIButton) {
    guard let title = textView.text, !title.isEmpty else {
        return
    }

    let todo = Todo(context: managedContext)
    todo.title = title
    todo.priority = Int16(segmentedControl.selectedSegmentIndex)
    todo.date = Date()

    do {
        try managedContext.save()
        dismiss(animated: true)
        textView.resignFirstResponder()

    } catch {
        print("Error saving todo: \(error)")

    }

}
@IBAction func cancelButton(_ sender: UIButton) {
    dismiss(animated: true)
    textView.resignFirstResponder() 
}

Any ideas what could have caused the app crash? Thanks

1
How do you declare textView? And the segmentedControl? If those are IBOutlets, make sure they're hooked up properly in the storyboard.LinusGeffarth
@LinusGeffarth IBOutlet weak var textView: UITextView!evgen
This is common error found for beginners .Search it in google you find Thousands of answerPrashant Tukadiya
Are they connected with the storyboard properly?LinusGeffarth

1 Answers

1
votes

UISegmentedControlSegment is the public enum and UISegmentedControl is the UIControl As per your comment, it seems that you have mistaken UISegmentedControl for UISegmentedControlSegment, so connect UISegmentedControl like below:

@IBOutlet weak var segmentedControl: UISegmentedControl!