requestLocation()在使用Swift 2的模拟器中总是失败

Man*_*imp 5 xcode cllocationmanager cllocation ios swift2

Swift 2和watchOS2完全改变了Apple Watch扩展的工作方式,现在我必须重新创建它们.最初,我曾以正常方式请求手表上的位置:

    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestAlwaysAuthorization()
    self.locationManager.beginUpdatingLocation()
Run Code Online (Sandbox Code Playgroud)

现在在watchOS2中,您只能使用"locationManager.requestLocation()"一次请求一个位置.它应该返回一个位置.这是我如何使用它:

override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)

    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestAlwaysAuthorization()
    self.locationManager.requestLocation() //crashes right here

}

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

}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print(error)
}
Run Code Online (Sandbox Code Playgroud)

它每次都崩溃,我无法弄清楚原因.我已经清理了构建,确保它正在模拟一个位置(startUpdatingLocation()在父应用程序上正常工作).我该怎么办?

编辑:这是堆栈跟踪:

2015-10-06 12:39:39.196 MyApp WatchKit    Extension[11486:2233547] *** Assertion failure in -[CLLocationManager requestLocation], /BuildRoot/Library/Caches/com.apple.xbs/Sources/CoreLocationFramework_Sim/CoreLocation-1861.1.12/Framework/CoreLocation/CLLocationManager.m:818
2015-10-06 12:39:39.198 MyApp WatchKit Extension[11486:2233547] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didFailWithError:'
*** First throw call stack:
(
    0   CoreFoundation                      0x002eaaf4 __exceptionPreprocess + 180
1   libobjc.A.dylib                     0x021e0df4 objc_exception_throw + 50
2   CoreFoundation                      0x002ea98a +[NSException raise:format:arguments:] + 138
3   Foundation                          0x007e3ad0 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 118
4   CoreLocation                        0x006864ec CLClientGetCapabilities + 12846
5   MyApp WatchKit Extension    0x00067a45 _TFC32MyApp_WatchKit_Extension27distanceInterfaceController16awakeWithContextfS0_FGSqPSs9AnyObject__T_ + 341
6   MyApp WatchKit Extension    0x00067afa _TToFC32MyApp_WatchKit_Extension27distanceInterfaceController16awakeWithContextfS0_FGSqPSs9AnyObject__T_ + 58
7   WatchKit                            0x0010f396 _WKInterfaceControllerCreateClass + 482
8   WatchKit                            0x000f7dc8 __48-[SPRemoteInterface handlePlist:fromIdentifier:]_block_invoke_3 + 366
9   libdispatch.dylib                   0x0438e7b7 _dispatch_call_block_and_release + 15
10  libdispatch.dylib                   0x043ac40d _dispatch_client_callout + 14
11  libdispatch.dylib                   0x0439505a _dispatch_main_queue_callback_4CF + 689
12  CoreFoundation                      0x0023cbee __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
13  CoreFoundation                      0x001fa964 __CFRunLoopRun + 2356
14  CoreFoundation                      0x001f9d76 CFRunLoopRunSpecific + 470
15  CoreFoundation                      0x001f9b8b CFRunLoopRunInMode + 123
16  Foundation                          0x00777601 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 308
17  Foundation                          0x0081d9cd -[NSRunLoop(NSRunLoop) run] + 82
18  libxpc.dylib                        0x046910b7 _xpc_objc_main + 486
19  libxpc.dylib                        0x04693e16 xpc_main + 215
20  Foundation                          0x00946c45 service_connection_handler + 0
21  PlugInKit                           0x038d81f4 -[PKService run] + 582
22  WatchKit                            0x0011df71 main + 146
23  libdyld.dylib                       0x043d0ae1 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 
Run Code Online (Sandbox Code Playgroud)

Dro*_*ppy 15

该错误消息告诉您尚未实现委托方法:

Delegate must respond to locationManager:didFailWithError:
Run Code Online (Sandbox Code Playgroud)

  • AHHHH.我把它包含在另一个文件中但是必须在这个文件中忘记它.我现在理解这种类型的错误以供将来参考,谢谢.它现在有效. (2认同)