0

I'm getting a "Fatal error: Unexpectedly found nil while unwrapping an Optional value" when I try to 'safely' unwrap a userdefault.

Previously I never had this problem and now when I've been forced to change over to a new view controller for this particular view (since I'm trying to redesign the UI), so I tried to use if let to safely unwrap, but the error still throws. To me, it looks like both switchState and newSwitchState have values but yet they still throw.

I've included a screenshot below. I'm a tad confused, but I have a nagging feeling that it is something simple I have overlooked.

Any help would be much appreciated.

Screen shot of fatal error

Aecasorg
  • 165
  • 2
  • 13
  • 1
    Please check that, have you bind the switch properly? – Khushbu May 28 '18 at 06:55
  • Please post code and error as text, not as pictures. – rmaddy May 28 '18 at 06:55
  • 2
    The outlet isn't connected. It has nothing to do with the `Bool` value. – rmaddy May 28 '18 at 06:56
  • 1
    Not related but there is no need to unwrap a `Bool` (or `Int`) value retrieved from `UserDefaults`. Just use `bool(forKey`. It returns a non-optional. – vadian May 28 '18 at 06:58
  • OK, I'll check that. Confusing error in that case. (Note: Why do my questions always get marked as duplicates when the duplicates never help and are not always particularly accurate to my question?) – Aecasorg May 28 '18 at 07:00
  • 1
    This is a duplicate because this is the 10,000th (exaggerating a little) time that someone has posted this exact same problem of an outlet not being connected. The duplicate covers this case. A few minutes with the debugger would have shown you that the outlet is `nil`. – rmaddy May 28 '18 at 07:03
  • Hmm, the outlets look connected. Do I need to reconnect them? I had use a container view to a navigation controller so I could get static cells. Is it ok to use one view controller for this? Can this view controller be used with both the view that has the container and the navigation controller within the container? – Aecasorg May 28 '18 at 07:05
  • Ok, sorry. Where would the debugger show that the outlet is nil? Thanks for help by the way! – Aecasorg May 28 '18 at 07:08
  • Set a breakpoint on the line causing the crash. Inspect the value of the variables when your app stops at the breakpoint, – rmaddy May 28 '18 at 07:13
  • As a matter of fact, I did that and I got exactly the same result (in the small window down to the left), so I pasted the actual error screenshot instead. I'll try again and see if I can pinpoint the exact cause of the error. Thanks for your help! – Aecasorg May 28 '18 at 07:36
  • OK, I've solved the issue. Firstly, I made two different view controllers, one for the view with the container and one separate for the navigation controller that the container view embeds. Secondly, I had a class named in the container view. If I left it empty, it all works. – Aecasorg May 30 '18 at 07:39

0 Answers0