Getting this error:
“fatal error: unexpectedly found nil while unwrapping an Optional value”
The error is coming up on the first line of the if statement.
I checked all the variables to see which value is optional but nothing is working. Basically, I want the text field to turn red when it is clicked and nothing is in the field. I am getting this error when running this code:
@IBOutlet weak var textEvent: UITextField!
@IBAction func addButtonAction(_ sender: Any) {
userData = true
UserDefaults.standard.set(userData, forKey: "userData")
if textEvent.text == "" {
textEvent.backgroundColor = UIColor.red
} else {
textEvent.backgroundColor = UIColor.white
event.append(textEvent.text!)
// UserDefaults.standard.set(event, forKey: "theEvent")
}
}
@IBAction func doneButton(_ sender: Any) {
dismiss(animated: true, completion: nil)
}