谷歌地图iOS版SDK通过GMSMapView类释放

问题描述:

我工作的谷歌地图iOS版SDK集成到我的项目,我不断收到此错误:谷歌地图iOS版SDK通过GMSMapView类释放

'NSInternalInconsistencyException' 'An instance 0x7fea5e93e210 of class GMSMapView was deallocated while key value observers were still registered with it. Current observation info:

这里是我的视图控制器代码:

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 
     } 
    } 

} 

正如它说:“关键价值观察员仍然在注册它”,这实质上是抱怨说你没有注销KVO观察员。

志愿,在这个意义上说,是有点像NSNotification - 如果你在,比方说,viewDidLoad:注册一个观察者,你需要删除,也就是说,viewDidDisappear:dealloc观察者。

对于您的情况,请尝试添加dealloc功能并使用removeObserver:forKeyPath:context:取消订阅KVO。阅读mattt的文章,了解更多KVO here

夫特3

deinit { 
      mapView.removeObserver(self, forKeyPath: "myLocation") 
}