简体   繁体   中英

Swift - [NSObject : AnyObject]!' is not a subtype of 'Dictionary<String, AnyObject>

I got this error message

[NSObject : AnyObject]!' is not a subtype of 'Dictionary<String, AnyObject>

My code

self.client.getAccessToken(code, success: { (accessTokenData:Dictionary<String, AnyObject>) -> Void in // error here
                    var accessToken = accessTokenData["access_token"]
                    self.requestMeWithToken(accessToken)
                    }, failure: { (error:NSError!) -> Void in
                    println("Quering accessToken failed \(error)")
                })

You can completely omit the type specification in your success completion. You might want something like this:

self.client.getAccessToken(code, success: { (accessTokenData) -> Void in // error here
    if let dict = accessTokenData as? Dictionary<String, AnyObject> {
        let accessToken = dict["access_token"]
        self.requestMeWithToken(accessToken)
    }
}, failure: { (error:NSError!) -> Void in
    println("Quering accessToken failed \(error)")
})

This will only work if the accessTokenData can successfully be cast to a [String: AnyObject] dictionary, though.

从错误消息中,您应该将Dictionary<String, AnyObject>更改为[NSObject : AnyObject]

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