Swift Map Variable Disappears

103 views Asked by At

I am trying to set up a map that zooms in on a location. Everything should be fine, but the map becomes unexpectedly nil and I can't figure out why.

@IBOutlet weak var map: MKMapView!

var numberOfAnnotations = 0

override func viewDidLoad() {

    numberOfAnnotations = 0

    var locationManager: CLLocationManager = CLLocationManager()

    map = MKMapView()

    println(map)

    locationManager.delegate = self

    locationManager.requestWhenInUseAuthorization()

    locationManager.startUpdatingLocation()

    map.showsUserLocation = true

    var location = locationManager.location.coordinate

    var span = MKCoordinateSpanMake(0.01, 0.01)

    println(location.latitude)
    println(location.longitude)

    var region = MKCoordinateRegion(center: location, span: span)

    println(region.center.longitude)
    println(region.span.latitudeDelta)
    println(map)

    map.setRegion(region, animated: true)

    println(map)
    println(map.description)

    var longPress = UILongPressGestureRecognizer(target: self, action: "addAnnotationToMap:")
    // duration required to place a pin is .75 seconds
    longPress.minimumPressDuration = 0.75


    map.addGestureRecognizer(longPress)

    map.delegate = self

    super.viewDidLoad()

}

The print lines that it produces are as follows and it crashes on the last println(map.description) and was crashing at map.addGestureRecognizer before that.

<MKMapView: 0x15fb0400; frame = (0 0; 0 0); clipsToBounds = YES; layer = <CALayer: 0x15fb0350>>
42.7881728503759
-86.1060027215381
-86.1060027215381
0.01
<MKMapView: 0x15fb0400; frame = (0 0; 0 0); clipsToBounds = YES; layer =<CALayer: 0x15fb0350>>
nil
fatal error: unexpectedly found nil while unwrapping an Optional value

Anyone have any idea why this would be happening?

0

There are 0 answers