Tom Xue Tom Xue - 8 months ago 68
Swift Question

Swift Error “Thread 1:EXC_BAD_INSTRUCTION" and fatal error: unexpectedly found nil while unwrapping an Optional value

Error"EXC_BAD_INSTRUCTION" happened and a fetal error "fatal error: unexpectedly found nil while unwrapping an Optional value". What does it mean and how do I fix it?

import UIKit
import MapKit
import CoreLocation

class SliderViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var travelRadius: UILabel!
@IBOutlet weak var mapKitView: MKMapView!
@IBOutlet weak var slider: UISlider!
private var lat : Double!
private var long : Double!
private var randomLatitude : Double!
private var randomLongtitude : Double!
let number1 : Double = 0.01449275362319 //Latitude 1 mile change
let number2 : Double = 0.01810190649279 //Longtitude 1 mile change
let random = Double(arc4random())/Double(UInt32.max) + 0

var locationManager : CLLocationManager!

var location : CLLocation!{
didSet {
lat = location.coordinate.latitude
long = location.coordinate.longitude

override func viewDidLoad() {


locationManager = CLLocationManager()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

self.mapKitView.showsUserLocation = true

@IBAction func sliderSlides(sender: AnyObject) {
let miles = Double(self.slider.value)
let delta = miles / 69.0
var currentRegion = self.mapKitView.region
currentRegion.span = MKCoordinateSpan(latitudeDelta: delta, longitudeDelta: delta)
self.mapKitView.region = currentRegion
travelRadius.text = "\(Int(round(miles))) mi"
randomLatitude = lat + number1*(round(miles))*random

randomLongtitude = long + number2*(round(miles))*(1-random)


func checkCoreLocationPermission() {
if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation() //Contains location details
} else if CLLocationManager.authorizationStatus() == .NotDetermined {
} else if CLLocationManager.authorizationStatus() == .Restricted {
print("Unauthorized to use location.")

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

location = locations.last

let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1.5,
longitudeDelta: 1.5))

self.mapKitView.setRegion(region, animated: false)

func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
print ("Errors:" + error.localizedDescription)

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "sendLocationData") {

let nav = segue.destinationViewController as! UINavigationController
let addEventViewController = nav.topViewController as! DestinationViewController
addEventViewController.latitude = randomLatitude
addEventViewController.longitude = randomLongtitude



Some Logs might be helpful


The message "unexpectedly found nil while unwrapping an Optional value" means just that, you are force-unwrapping an optional that is nil.

In this case, you define lat as: private var lat : Double! where the ! indicates to the compiler that the variable should be automatically unwrapped. This variable is not getting set before it is being used here: randomLatitude = lat + number1*(round(miles))*random, resulting in the crash.

It looks like the view is being loaded before location is being set, or possibly location is nil. See your screen shot:

your screen shot