0

我正在尝试设置放大某个位置的地图。一切都应该没问题,但是地图出乎意料地变成了零,我不知道为什么。

@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()

}

它产生的打印行如下,它在最后一个 println(map.description) 崩溃,并且在此之前在 map.addGestureRecognizer 崩溃。

<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

任何人都知道为什么会发生这种情况?

4

0 回答 0