Failing with “unexpectedly found nil” error (MyLocations) [SOLVED]

Hi iOS gurus,

Now I’m failing to complete the final step on P.165 in iOS Apprentice 3(MyLocations) with:
fatal error: unexpectedly found nil while unwrapping an Optional value

When I press the Done button, my app fails at the following line in done()
let location =
NSEntityDescription.insertNewObjectForEntityForName(“Location”, inManagedObjectContext: managedObjectContext) as! Location

I reviewed my codes again and again, but still have no idea what’s going on.

Is there anybody who experienced similar issues?
I’d appreciate it if you would give me any hints for troubleshooting…

Many thanks in advance for your help.

Most likely, the value of managedObjectContext is nil. This happens when you forget to set it in prepareForSegue, or forgot to give the segue an identifier so that this code is skipped in prepareForSegue.

Thank you very much for your help!!

You solved the issue!! The cause was that I was missing the segue ID “TagLocation” set.
I feel ashamed that I haven’t had enough practices…

I’ll continue my “journey”. :slight_smile:


It’s a very common mistake. I do it all the time myself, so don’t feel too bad about it. :wink: