我正在努力将GoogleMaps iOS SDK集成到我的项目中,并且我一直收到此错误:
'NSInternalInconsistencyException''实例0x7fea5e93e210 当键值观察者仍然存在时,类GMSMapView被释放 在它注册。目前的观察信息:
这是我的视图控制器代码:
import UIKit
import GoogleMaps
class ViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {
var locationManager = CLLocationManager()
var didFindMyLocation = false
let mapView = GMSMapView()
override func viewDidLoad() {
super.viewDidLoad()
let mapView = GMSMapView()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
let camera: GMSCameraPosition = GMSCameraPosition.cameraWithLatitude(37.61729,longitude: -122.38229, zoom: 18)
mapView.camera = camera
mapView.delegate = self
mapView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.New, context: nil)
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if !didFindMyLocation {
let myLocation: CLLocation = change![NSKeyValueChangeNewKey] as! CLLocation
mapView.camera = GMSCameraPosition.cameraWithTarget(myLocation.coordinate, zoom: 18.0)
mapView.settings.myLocationButton = true
didFindMyLocation = true
}
}
}
答案 0 :(得分:1)
正如它所说的那样:&#34;关键价值观察者仍在注册#34;这实质上是在抱怨你没有注册KVO观察员。
从这个意义上讲,KVO有点像NSNotification
- 如果您在viewDidLoad:
注册观察者,则需要删除观察者,例如viewDidDisappear:
或{ {1}}。
在您的情况下,尝试添加dealloc
功能并使用dealloc
取消订阅KVO。有关KVO here的文章,请参阅mattt的文章。
答案 1 :(得分:1)
Swift 3
deinit {
mapView.removeObserver(self, forKeyPath: "myLocation")
}