CLLocationManager - 致命错误:在展开Optional值时意外发现nil

时间:2015-09-24 10:41:23

标签: cllocationmanager swift2 xcode7 clgeocoder

我对CLLocationManager有一点问题。当我点击IBAction“addPlace”时,应用程序崩溃了。我没有看到问题,我不明白应用程序崩溃的原因。

提前感谢您的回复。

class Map: UIViewController, CLLocationManagerDelegate {

    // Outlets

    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var BTNAdd: UIButton!

    // Variables

    var positionManager = CLLocationManager()
    var geoCoder : CLGeocoder!
    var placemark : CLPlacemark!

    var name: String = ""
    var address: String = ""
    var longitude: String = ""
    var latitude: String = ""

    // Actions

    @IBAction func myPosition(sender: AnyObject) {

        positionManager.delegate = self
        positionManager.desiredAccuracy = kCLLocationAccuracyBest
        positionManager.requestWhenInUseAuthorization()
        positionManager.startUpdatingLocation()

        mapView.showsUserLocation = true

    }

    @IBAction func addPlace(sender: AnyObject) {

            ///////////////////////////////////////
            // BUG HERE
            ///////////////////////////////////////

            let locality = String(self.placemark.locality)
            let postalCode = String(self.placemark.postalCode)
            let administrativeArea = String(self.placemark.administrativeArea)
            let country = String(self.placemark.country)

            print(locality + postalCode + administrativeArea + country)

            ///////////////////////////////////////
            ///////////////////////////////////////

    }

    // Functions

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

        CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in

            if let pm = placemarks?.first {
                self.displayLocationInfo(pm)
            }
        })

    }

    func displayLocationInfo(placemark: CLPlacemark?) {
        if let containsPlacemark = placemark {

            positionManager.stopUpdatingLocation()

            let locality = (containsPlacemark.locality != nil) ? containsPlacemark.locality : ""
            let postalCode = (containsPlacemark.postalCode != nil) ? containsPlacemark.postalCode : ""
            let administrativeArea = (containsPlacemark.administrativeArea != nil) ? containsPlacemark.administrativeArea : ""
            let country = (containsPlacemark.country != nil) ? containsPlacemark.country : ""

        }

    }

}

2 个答案:

答案 0 :(得分:2)

您的placemark变量被声明为显式解包的可选项,但由于您收到此错误消息,很明显有时此变量将为nil。

更改声明以使其成为正常可选,然后使用if let确保安全展开并处理错误:

var placemark : CLPlacemark?

@IBAction func addPlace(sender: AnyObject) {

        if let mark = placemark {
            let locality = String(self.mark.locality)
            let postalCode = String(self.mark.postalCode)
            let administrativeArea = String(self.mark.administrativeArea)
            let country = String(self.mark.country)
            print(locality + postalCode + administrativeArea + country)
        } else {
            print("placemark was nil")
        }

}

答案 1 :(得分:0)

我在课堂上添加了变量,并修复了我的IBAction

var locality: String = ""
var postalCode: String = ""
var administrativeArea: String = ""
var country: String = ""

@IBAction func addPlace(sender: AnyObject) {

    let locality = self.locality
    let postalCode = self.postalCode
    let administrativeArea = self.administrativeArea
    let country = self.country

    print(locality + postalCode + administrativeArea + country)

}