简体   繁体   中英

Cannot subscript a value of type '[NSObject : AnyObject]' with an index of type 'NSKeyValueChangeKey'

I am getting this error when I am putting this code.

Error: Cannot subscript a value of type '[NSObject : AnyObject]' with an index of type '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
    }
}

This is the correct signature of observeValue(forKeyPath:of:change:context:) (Swift 4):

func observeValue(forKeyPath keyPath: String?, 
                of object: Any?, 
           change: [NSKeyValueChangeKey : Any]?, 
          context: UnsafeMutableRawPointer?)

Regardless of language version, the key for you is to change change: [NSObject : AnyObject] to change: [NSKeyValueChangeKey : Any]? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM