0

I am new to swift and have a small amount of code that I just can't seem to get to work:

if Tick1.state == NSOnState {
    Textmain4.stringValue = "Quality Enabled"
}

This if statement spits out

fatal error: unexpectedly found nil while unwrapping an Optional value

What do I need to do to fix this?

Tick1 is an NSButton and Textmain4 is an NSTextField.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Dylan
  • 3
  • 3
  • 1
    Place a breakpoint on the `if` statement. When it hits it, see which is `nil`: `Tick1` or `Textmain4`. It's very obvious that one of them is. Also, learn how to use Swift conventions - class instances use camel-case. :-) Good luck! –  Aug 17 '17 at 18:15

1 Answers1

0

If Tick1 or Textmain4 is an implicitly unwrapped optional (declared with an !, like let Tick1: UIButton!), you can still use ? to safely unwrap the object:

if Tick1?.state == NSOnState {
    Textmain4?.stringValue = "Quality Enabled"
}
Cal Stephens
  • 725
  • 9
  • 20