我正在尝试设置放大某个位置的地图。一切都应该没问题,但地图出乎意料地为零,我无法弄清楚原因。
@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
任何人都知道为什么会发生这种情况?