Search code examples
iosswiftxcodetestflight

Swift Map Kit unexpectedly found nil while unwrapping an Optional value


I sent my working app out to Test flight and in order to get it to compile correctly I changed the following things:

  • Enabled full screen
  • Turned off left and right orientation

I found my test flight copy had a bug so I went to test it in the Simulator and I am now presented with the following error:

fatal error: unexpectedly found nil while unwrapping an Optional value

it highlights the line:

MapView.setRegion(MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(-39.0556253, 174.0752278), distanceSpan,distanceSpan), animated: true)

This worked perfectly fine before I compiled it and ran it in testflight. What could've happened with test flight to cause this? Is there anyway to revert to the code before I archived it? I have previous archives can I run the code from those instead?

Thanks for your help


Solution

  • Broken outlet connections often cause "unexpectedly found nil while unwrapping optional" errors in Swift because by default outlets are set up as force-unwrapped optionals. If they are nil, every code reference to the outlet will crash.

    The code you posted shows that it is DEFINED as an outlet, but not that it is connected.

    Open the scene in your storyboard. Select the connections inspector. Select the map. Look to see if it shows as connected to an outlet in your view controller. Probably not. Next, select the view controller's source file, in the assistant editor, find the outlet, and control-drag from the circle to the left of the outlet in your code onto the map view to connect the outlet.