Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法为类型为';[NSObject:AnyObject]';索引类型为';NSKeyValueChangeKey';_Ios_Swift_Google Maps - Fatal编程技术网

Ios 无法为类型为';[NSObject:AnyObject]';索引类型为';NSKeyValueChangeKey';

Ios 无法为类型为';[NSObject:AnyObject]';索引类型为';NSKeyValueChangeKey';,ios,swift,google-maps,Ios,Swift,Google Maps,当我输入此代码时,我收到此错误 错误:无法使用“NSKeyValueChangeKey”类型的索引为“[NSObject:AnyObject]”类型的值下标 func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutableRawPointer) { if !didFindMyLocation {

当我输入此代码时,我收到此错误

错误:无法使用“NSKeyValueChangeKey”类型的索引为“[NSObject:AnyObject]”类型的值下标

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

这是观察值的正确签名(forKeyPath:of:change:context:)(Swift 4):


无论语言版本如何,关键是将
更改:[NSObject:AnyObject]
更改为
更改:[NSKeyValueChangeKey:Any]?

这是
observeValue(forKeyPath:of:change:context:)的正确签名。
(Swift 4):


无论语言版本如何,您的关键是将
change:[NSObject:AnyObject]
更改为
change:[NSKeyValueChangeKey:Any]?

在Swift3中将
AnyObject
的类型替换为
Any
。因此,我认为Tom的回答是正确的。在Swift3中,
AnyObject
的类型被
Any
的类型所取代。所以,我认为汤姆的回答是正确的。
func observeValue(forKeyPath keyPath: String?, 
                of object: Any?, 
           change: [NSKeyValueChangeKey : Any]?, 
          context: UnsafeMutableRawPointer?)