Search code examples
xcodeswiftnstextfield

Error: Failed to connect outlet from ... to (NSTextField): missing setter or instance variable


Why does this code:

if Note1Math.stringValue == "" {         
        TxtFilled = 0

        }else{

        TxtFilled = 1

    }

give this error?:

2015-06-18 20:20:17.633 Office[41763:430750] Failed to connect (Note1) outlet from (Office.ViewController) to (NSTextField): missing setter or instance variable.
fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)


Solution

  • This part of the message:

    2015-06-18 20:20:17.633 Office[41763:430750] Failed to connect (Note1) outlet from (Office.ViewController) to (NSTextField): missing setter or instance variable.

    does not come from that code. It comes from the loading of a NIB or storyboard. Presumably, you had named an outlet Note1 at one time, connected it in the NIB or storyboard, and then renamed it to Note1Math in the code without fixing the NIB/storyboard.

    Then, later, when you accessed Note1Math, it was nil (because it was not connected in the NIB/storyboard). That caused the second message:

    fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

    The solution is to go into the NIB or storyboard, disconnect the outlet with the old name, and reconnect the outlet.