当我转换为swift 3时,它的说法类型没有下标的成员。
let dataDictionary = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)
let accessToken = dataDictionary["access_token"] as! AnyObject?
我尝试了许多可能性,但没有奏效。
答案 0 :(得分:0)
JSONSerialization.jsonObject(with:options:)
的返回类型为Any
,不允许下标。您必须使用显式类型转换为下标。如果您尝试将数据转换为[String: Any]
,则可以执行以下操作:
if let dataDictionary = dataDictionary as? [String: Any] {
// dataDictionary["access_token"] as AnyObject
}
答案 1 :(得分:0)
JSONSerialization.jsonObject
会抛出错误,所以请在do catch
块
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)
}