-2

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)
}
rmaddy
  • 307,833
  • 40
  • 508
  • 550
drake
  • 1
  • 2
  • What is this textEvent variable? Where is it defined? If its an outlet, have you checked that its properly bound no the nib? – Caio Sym Nov 07 '17 at 03:11
  • There are several ways to find the offending line. Probably the easiest - and provided you've given us all your code it's down to *one* line - is to find out where you are *unwrapping* something. (Look for any and all `!`.) You only have one - `textEvent.text!`. Now the question becomes simple - why is it `nil`? Maybe because `textEvent` is? Put a breakpoint on that line, and find out if this is the case. Finally, please add where you are declaring `textEvent` - I'm betting it's an `@IBOutlet` and it's either (a) not connected properly or (b) declared as `weak` and is somehow being released. –  Nov 07 '17 at 03:13
  • @CaioSym I just added it to the first line of my code. – drake Nov 07 '17 at 03:24
  • @dfd I just added the line. it is the first line of code – drake Nov 07 '17 at 03:25
  • So have you checked if it's `nil`? Have you searched for questions similar to your's here on SO? There are several of them.... –  Nov 07 '17 at 03:30
  • @dfd How do I check that? I did but none of them made sense. Do I need to define it a certain way? – drake Nov 07 '17 at 03:31
  • @drake you should be able to check it in the xib´s outlet inspector(last tab) when selecting the view controller. Alternativelly, the editor should have a small circle next to each line of code. if that circle is full your outlet is set. else its not set – Caio Sym Nov 07 '17 at 03:34
  • Added a link to the answer so you can have some visual aids :) – Caio Sym Nov 07 '17 at 03:36
  • Well, as I'm seeing (and said) there are several ways. My way? Set a breakpoint on the very *first* reference to `textEvent`. (And if you don't know what I'm saying, please learn about "breakpoint" in "Xcode".) Expand `self' and see what the value for everything is. As I said in an earlier comment, most of this is either (a) been asked before here or (b) has *several* online resources to walk you through it. I'm not meaning to be snarky but questions like this are asked - by average - a half dozen (or more) times a week and I'm trying to guide you to *learn how to fish*. –  Nov 07 '17 at 05:13

2 Answers2

1

The problem seems to be with

@IBOutlet weak var textEvent: UITextField!

Ensure that the outlet is properly set from your nib.

Here is a guide to help you with connecting the offending outlet: https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ConnectTheUIToCode.html

Caio Sym
  • 364
  • 1
  • 7
0

Try

if textEvent != nil {  
       ...(do whatever you need to do)
    }

However if you really want to get fancy you can also do option binding,

if let myTextEvent = textEvent {  
           ...(do whatever you need to do, but use myTextEven instead of textEvent inside this if statement)
        }
iOSGuy93892
  • 139
  • 7
  • The error is probably due to textEvent being declared as an implicitly unwrapped optional. This check won´t help in that case. – Caio Sym Nov 07 '17 at 03:12
  • 1
    Even worse, it doesn't address the actual issue - it merely *hides* it. –  Nov 07 '17 at 03:29