-5

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.
}
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
g_1_k
  • 484
  • 1
  • 6
  • 13

1 Answers1

2

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

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • By the way, if all you want to do is have the map view track the user's location, what you're doing is _not_ the way to do it. – matt Feb 18 '16 at 21:31
  • you mean drag and drop? – g_1_k Feb 18 '16 at 21:33
  • Do you have any insight as to why has Apple has not made this a compilation error? When would someone ever benefit from not linking an outlet? It would prevent this question from being asked every day. – Caleb Feb 18 '16 at 21:45
  • Because that's not how nibs work. We won't know there's a problem until the nib has loaded and you try to refer to the nib-instantiated object thru the outlet property. That's not compile time, it's runtime. – matt Feb 18 '16 at 21:49