Ambiguous reference to member 'subscript'

2019-02-25 14:44发布

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!

标签: swift3
1条回答
Viruses.
2楼-- · 2019-02-25 15:28

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].

查看更多
登录 后发表回答