当我输入此代码时,我收到此错误。
错误:无法将类型为"[NSObject:AnyObject]"类型的值下标为"NSKeyValueChangeKey"的索引
func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutableRawPointer) {
if !didFindMyLocation {
let myLocation: CLLocation = change[NSKeyValueChangeNewKey] as CLLocation
viewMap.camera = GMSCameraPosition.camera(withTarget: myLocation.coordinate, zoom: 10.0)
viewMap.settings.myLocationButton = true
didFindMyLocation = true
}
}
这是observeValue(forKeyPath:of:change:context:)
(Swift 4( 的正确签名:
func observeValue(forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey : Any]?,
context: UnsafeMutableRawPointer?)
无论语言版本如何,您的关键是将change: [NSObject : AnyObject]
更改为change: [NSKeyValueChangeKey : Any]?
.