-3

When i converted to swift 3 , its saying type any has no subscript memebers.

let dataDictionary = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)
let accessToken = dataDictionary["access_token"] as! AnyObject?

Ihave tried many possiblities but didn't worked.

user2702179
  • 187
  • 2
  • 13

2 Answers2

0

The return type of JSONSerialization.jsonObject(with:options:) is Any which doesn't allow subscripting. You have to use explicit type conversion to subscript. If you're trying to cast the data to [String: Any] you could do the following:

if let dataDictionary = dataDictionary as? [String: Any] {
    // dataDictionary["access_token"] as AnyObject
}
Aaron
  • 6,466
  • 7
  • 37
  • 75
0

JSONSerialization.jsonObject throws an error so let catch it in a do catch block

    do {
        let dataDictionary = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String: Any]
        let accessToken = dataDictionary["access_token"] as! AnyObject
    } catch let error as NSError {
        print(error)
    }
Danh Huynh
  • 2,337
  • 1
  • 15
  • 18