全部,
我有一个有效的KVO请求。我有一个位置类和一个视图控制器。当在视图控制器上按下按钮时,它会运行位置类以获得GEOLOCATION。然后它触发一个KVO,视图控制器获取更改,然后它们进入视图控制器中的标签。
我收到此错误:
An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
这是我的代码: 在locationManager类中:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let location = locations.last as! CLLocation
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, e) -> Void in
if let error = e {
println("Error: (e.localizedDescription)")
} else {
let placemark = placemarks.last as! CLPlacemark
self.LocationString = "\(placemark.subLocality), \(placemark.locality)"
self.addObserver(self, forKeyPath: "LocationString", options: .New, context: nil)
self.LocationManager.stopUpdatingLocation()
}
})
在我的视图控制器中:
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject: AnyObject], context: UnsafeMutablePointer<Void>) {
}
答案 0 :(得分:-1)
就像苹果文档“将Swift与Cocoa和Objective-c一起使用”所说,你需要删除 deinit中的观察者
deinit{
self.removeObserver(self, forKeyPath: , context:);
}