为什么未调用locationManager(:didUpdateLocations)

时间:2019-01-19 20:34:54

标签: swift cllocationmanager

我写了以下测试代码

 @IBAction func btnLocation(_ sender: NSButton) {
    let locManager = CLLocationManager()
    if CLLocationManager.authorizationStatus() != .authorized {
        print("not authorized")
    }
    locManager.delegate = self
    locManager.desiredAccuracy = kCLLocationAccuracyBest
    locManager.startUpdatingLocation()
    locManager.requestLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations[0]
    print(location.coordinate.latitude)
    manager.stopUpdatingLocation()
    let geoCoder = CLGeocoder()
    geoCoder.reverseGeocodeLocation(location, completionHandler: {(places, error) in
        let place: CLPlacemark = places![0]
        print(place.administrativeArea!)
    })
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    print(error)
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    print("authorization changed")
}

但不调用locationManager(:didUpdateLocations)并且不会打印错误。如果在开始工作中删除了locationManager(:didFailWithError)方法,并且打印了位置坐标,但是Xcode还声称“委托必须响应locationManager:didFailWithError: 我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

您需要制作

let locManager = CLLocationManager()

实例var可以为委托保存强大的引用,您也需要其中之一

locManager.startUpdatingLocation()
locManager.requestLocation()