gpt4 book ai didi

ios - GoogleMaps iOS SDK GMSMapView 类解除分配

转载 作者:搜寻专家 更新时间:2023-10-31 22:46:02 25 4
gpt4 key购买 nike

我正在努力将 GoogleMaps iOS SDK 集成到我的项目中,但我不断收到此错误:

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

这是我的 View Controller 代码:

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 观察者。

从这个意义上说,KVO 有点像 NSNotification - 如果您在 viewDidLoad: 中注册了一个观察者,您需要在其中删除观察者,例如, viewDidDisappear:dealloc

在您的情况下,尝试添加 dealloc 函数并使用 removeObserver:forKeyPath:context: 取消订阅 KVO。在 mattt 的 KVO 文章中阅读更多信息 here .

关于ios - GoogleMaps iOS SDK GMSMapView 类解除分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37957412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com