解析json时对成员'subscript'的不明确引用 [英] Ambiguous reference to member 'subscript' when parsing json

查看:166
本文介绍了解析json时对成员'subscript'的不明确引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚更新到Xcode 8.0/Swift 3,并且收到了此消息

I just updated to Xcode 8.0/Swift 3 and I'm getting this message

对成员下标"的含糊不清

Ambiguous reference to member 'subscript'

在此行:

let rootResults = rootDictionary ["results"] 如? [[NSObject:AnyObject]]

let rootResults = rootDictionary["results"] as? [[NSObject: AnyObject]]

其余代码如下所示:

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)
    }
}

我看过其他与此类似的问题,但它们与json解析无关. 我将不胜感激. 谢谢!

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!

推荐答案

在Swift 3中,大多数隐式类型转换都被删除. 这使得String文字无法自动转换为NSObject.

In Swift 3, most implicit type conversions are removed. That has made String literals unable to be automatically converted to NSObject.

尝试将代码中的[NSObject: AnyObject]替换为[String: AnyObject].

Try replacing [NSObject: AnyObject] in your code to [String: AnyObject].

这篇关于解析json时对成员'subscript'的不明确引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆