iosswiftexc-bad-instruction

Swift error EXC_BAD_INSTRUCTION


Please I have this code. I'm new to Swift (started only today). What's the problem here? I tried searching stack but found only Objective-C solutions.

class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager = CLLocationManager()

@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()
    locationManager.delegate = self

    if (CLLocationManager.locationServicesEnabled()) {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    let location = locations.last! as CLLocation

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

    mapView.setRegion(region, animated: true)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

Solution

  • You forgot to hook the map view in the storyboard to your mapView outlet in code, probably.