简体   繁体   English

Swift中的watchValueForKeyPath崩溃的应用程序

[英]observeValueForKeyPath crashing app in swift

I'm very new to iOS development and trying to wrap my head around why my app is crashing with a message of SIGABRT. 我对iOS开发非常陌生,并试图用SIGABRT消息来说明为什么我的应用程序崩溃。 I am following along a tutorial ( link ) on how to implement Google Maps SDK and as far as I can tell I have the same code. 我将按照教程( 链接 )进行操作,以了解如何实现Google Maps SDK,据我所知,我具有相同的代码。

My code: 我的代码:

@IBOutlet weak var mapView: GMSMapView!

let locationManager = CLLocationManager()
var didFindMyLocation = false

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    locationManager.requestWhenInUseAuthorization()
    locationManager.delegate = self

    mapView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.New, context: nil)

    }

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == CLAuthorizationStatus.AuthorizedWhenInUse {
        mapView.myLocationEnabled = true
    }
}

private func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
    if !didFindMyLocation {
        let myLocation: CLLocation = change[NSKeyValueChangeNewKey] as! CLLocation
        mapView.camera = GMSCameraPosition.cameraWithTarget(myLocation.coordinate, zoom: 10)
        mapView.settings.myLocationButton = true

        didFindMyLocation = true
    }
}

The crash happens as I add the last function observeValueForKeyPath and I cannot figure out why. 当我添加最后一个函数observeValueForKeyPath时发生崩溃,我无法弄清原因。 I get the following error message: 我收到以下错误消息:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: ': An -observeValueForKeyPath:ofObject:change:context: message was received but not handled. 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:':收到但未处理-observeValueForKeyPath:ofObject:change:context:消息。 Key path: myLocation 关键路径:myLocation

Can someone tell me what I'm doing wrong? 有人可以告诉我我在做什么错吗?

The problem is that you haven't declared observeValueForKeyPath(_:ofObject:change:context:) correctly. 问题是您没有正确声明observeValueForKeyPath(_:ofObject:change:context:) Your method expects the wrong argument types for keyPath , ofObject , and change . 您的方法期望keyPathofObjectchange参数类型错误。

The fact that you didn't declare your method override is a clue: if you had declared it with the correct types, the compiler would tell you that it also needs to be declared override . 您没有声明方法override的事实是一个线索:如果您使用正确的类型声明了它,编译器会告诉您还需要声明它的override (It would also tell you that you cannot declare it private , because it's public in NSObject .) (这还会告诉您,您不能将其声明为private ,因为它在NSObjectpublic的。)

The correct declaration looks like this: 正确的声明如下所示:

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?,
    change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>)

If you just start to type the method name at class scope, Xcode will offer to autocomplete the correct declaration: 如果您只是开始在类范围内键入方法名称,则Xcode将提供自动完成正确声明的方法:

Xcode自动完成

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

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