【问题标题】:Error when trying to get user location (swift)尝试获取用户位置时出错(swift)
【发布时间】:2017-03-24 22:36:43
【问题描述】:

我正在尝试获取用户在 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: 已实现,但我有同样的错误。

【问题讨论】:

  • 这里他们说这是 8.1 xcode 中的一个错误。 stackoverflow.com/questions/39495773/… 你的 xcode 版本是多少。也许你需要更新。
  • 你对 locationManager 的定义是什么?
  • @hasan83 我有 xcode 8.2.1
  • @C6Silver 让 locationManager = CLLocationManager()
  • 包含类是否继承自 NSObject?从 NSObject 继承后,我能够在没有警告的情况下构建它。

标签: ios swift xcode cllocationmanager


【解决方案1】:

改成

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

对我的情况有所帮助

【讨论】:

  • 所以现在没有崩溃?
  • @hasan83 是的,没有崩溃,但我仍然有这个警告
猜你喜欢
  • 1970-01-01
  • 2016-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-29
  • 2016-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多