swift3 - 解析 json 时对成员 'subscript' 的引用不明确

标签 swift3

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

Ambiguous reference to member 'subscript'

在这一行:

让 rootResults = rootDictionary["results"] 为? [[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 解析无关。 我将不胜感激任何帮助。 谢谢!

最佳答案

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

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

关于swift3 - 解析 json 时对成员 'subscript' 的引用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39427214/

相关文章:

swift - 在所有成员初始化之前被闭包自捕获

ios - collectionview scrollToItemAtIndexPath 崩溃应用程序

ios - 从 View 中删除 UIView

ios - 我使用 coreData 保存数据,但对象仍然为零

swift - 字符串到 C 字符** Swift 3

ios - 重新发送 Firebase 电子邮件验证

ios - 带换行符 (\n) 的 AttributedString 无法正常工作

ios - 我不能在 swift 3 中对请求使用 Alert inside response

ios - 用图像填充文本

swift - 条件绑定(bind)的初始化程序必须具有 Optional 类型,而不是 '[String : Any]'