简体   繁体   English

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

[英]Error when trying to get user location (swift)

I am trying to get user's location on iOS. 我正在尝试获取用户在iOS上的位置。 I use CLLocationManagerDelegate for it. 我为此使用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)")
}

But I get this error 但是我得到这个错误

2017-03-24 18:03:57.183 *** Assertion failure in -[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.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 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didFailWithError:' 2017-03-24 18:03:57.194 ***由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'代理必须响应locationManager:didFailWithError:'

After implementing locationManager:didFailWithError: 实施locationManager:didFailWithError:

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

I have this warning 我有这个警告

Instance method 'locationManager( :didFailWithError:)' nearly matches optional requirement 'locationManager( :didFailWithError:)' of protocol 'CLLocationManagerDelegate' 实例方法“ locationManager( :didFailWithError :)”几乎与协议“ CLLocationManagerDelegate”的可选要求“ locationManager( :didFailWithError :)” 匹配

And again this error 再一次这个错误

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didFailWithError:' ***由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“代理必须响应locationManager:didFailWithError:”

I dont know what incorrect. 我不知道什么不对。 locationManager:didFailWithError: implemented, but I have same error. locationManager:didFailWithError:实现,但我有同样的错误。

changing to 更改为

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

helped in my situation 在我的情况下有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM