I just updated to Xcode 8.0/Swift 3 and I'm getting this message
Ambiguous reference to member 'subscript'
on this line:
let rootResults = rootDictionary["results"] as? [[NSObject: AnyObject]]
The rest of the code is shown below:
func parseJSON(data: NSData) {
do {
let json = try JSONSerialization.jsonObject(with: data as Data, options: .mutableContainers)
if let rootDictionary = json as? [NSObject: AnyObject],
let rootResults = rootDictionary["results"] as? [[NSObject: AnyObject]]
{
for childResults in rootResults {
if let firstName = childResults["first_name"]! as? String {
let customerFirstName = firstName
customerData["firstName"] = customerFirstName
}
if let lastName = childResults["middle_name"]! as? String {
let customerLastName = lastName
customerData["middleName"] = customerLastName
}
if let lastName = childResults["last_name"]! as? String {
let customerLastName = lastName
customerData["lastName"] = customerLastName
}
if let nameSuffix = childResults["name_suffix"]! as? String {
let customerSuffix = nameSuffix
customerData["nameSuffix"] = customerSuffix
}
}
}
} catch {
print(error)
}
}
I have looked at other questions similar to this but they are about something unrelated to json parsing. I would appreciate any help on this. Thanks!
In Swift 3, most implicit type conversions are removed. That has made String literals unable to be automatically converted to
NSObject
.Try replacing
[NSObject: AnyObject]
in your code to[String: AnyObject]
.