CLLocationManager:在展开可选值时意外发现 Nil

CLLocationManager: Unexpectedly Found Nil While Unwrapping an Optional Value

这是我在 ViewController.swift 中的代码:

import UIKit
import Alamofire
import SwiftyJSON
import MapKit
import CoreLocation
import TwitterKit

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet weak var myMap: MKMapView!
    @IBOutlet weak var counterLabel: UILabel!

    var manager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        //calling CLLocation
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()

        //map region
        let locationZoom = self.manager.location
        var latitude: Double = locationZoom.coordinate.latitude
        var longitude: Double = locationZoom.coordinate.longitude
        var centerLatitude: CLLocationDegrees = locationZoom.coordinate.latitude
        var centerLongitude: CLLocationDegrees = locationZoom.coordinate.longitude
        var latDelta: CLLocationDegrees = 0.03
        var longDelta: CLLocationDegrees = 0.03
        var span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
        var location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(centerLatitude, centerLongitude)
        var region: MKCoordinateRegion = MKCoordinateRegionMake(location, span) // this creates a map region with the center
        myMap.setRegion(region, animated: true)
    }

    @IBAction func zoom(sender: AnyObject) {


        let locationZoom = self.manager.location

        var latitude: Double = locationZoom.coordinate.latitude
        var longitude: Double = locationZoom.coordinate.longitude
        var centerLatitude:CLLocationDegrees = locationZoom.coordinate.latitude
        var centerLongitude: CLLocationDegrees = locationZoom.coordinate.longitude
        var latDelta:CLLocationDegrees = 0.03
        var longDelta:CLLocationDegrees = 0.03
        var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
        var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(centerLatitude, centerLongitude)
        var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span) // this creates a map region with the center
        myMap.setRegion(region, animated: true)
    }
}

现在,错误描述如下:

致命错误:在展开可选值时意外发现 nil

线程 1 指向这个问题:

var latitude: Double = locationZoom.coordinate.latitude

但是,在模拟器上进行测试时,我也发现了经度变量的问题。尽管如此,我还是不明白为什么经度和纬度的值都设置为 "nil." 我已经正确设置了 CLLocationManager,确保根据需要修改 info.plist 以包含 NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription,等等。这是怎么回事,我该如何解决这个问题?

发生这种情况是因为当您的视图加载时它没有收到任何位置更新。实施 DidUpdateLocations 并在那里访问位置信息。如果您需要参考,可以查看 DidUpdateLocations

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

    let locationZoom = locations.last as! CLLocation
    let latitude: Double = locationZoom.coordinate.latitude
    let longitude: Double = locationZoom.coordinate.longitude
    println(latitude)
    println(longitude)
}