尝试获取用户位置时出错(swift)

时间:2017-03-24 22:36:44

标签: ios swift xcode cllocationmanager

我正在尝试获取用户在iOS上的位置。 我使用CLLocationManagerDelegate

if CLLocationManager.locationServicesEnabled() {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    locationManager.requestLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations.first
    print("locations = \(location?.coordinate.latitude) \(location?.coordinate.longitude)")
}

但是我收到了这个错误

  

2017-03-24 18:03:57.183 ***断言失败 - [CLLocationManager requestLocation],        /BuildRoot/Library/Caches/com.apple.xbs/Sources/CoreLocationFramework_Sim/CoreLocation-2100.0.34/Framework/CoreLocation/CLLocationManager.m:867
      2017-03-24 18:03:57.194 ***由于未捕获的异常终止应用程序' NSInternalInconsistencyException',        原因:'委托必须响应locationManager:didFailWithError:'

实施locationManager:didFailWithError:

之后
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    print(error.description)
}

我有这个警告

  

实例方法' locationManager(:didFailWithError :)'几乎匹配可选要求' locationManager(:didFailWithError :)'        协议' CLLocationManagerDelegate'

再次出现此错误

  

***由于未捕获的异常终止应用程序' NSInternalInconsistencyException',        原因:'委托必须响应locationManager:didFailWithError:'

我不知道什么不对。 locationManager:didFailWithError:已实施,但我也有同样的错误。

1 个答案:

答案 0 :(得分:0)

更改为

@objc(locationManager:didFailWithError:)
func locationManager(_ manager: CLLocationManager, didFailWithError error: NSError) {
    print(error.localizedDescription)
}

在我的情况下有所帮助