【问题标题】:"Ambiguous reference to member 'subscript'"“对成员'下标'的模糊引用”
【发布时间】:2017-09-12 18:28:14
【问题描述】:

我刚刚遇到了几个 Ambiguous reference to member 'subscript' 错误,这些错误没有被现有答案解决。这段代码说明了问题:

enum Keys: String {
    case items = "items"
    // …
}

if let parseJSON = try JSONSerialization.jsonObject(with: data) as? [String:Any],
    let items = parseJSON[Keys.items] as? [[String:Any]]
{
    // …
}

parseJSON[Keys.items] 上产生错误。

【问题讨论】:

  • 无需为您的案例设置完全相同的字符串表示形式
  • @LeoDabus 谢谢。碰巧的是,并非所有枚举案例的值都与其名称相同,我只是删除了大多数枚举案例以使其更具可读性。

标签: swift


【解决方案1】:

问题是Keys.itemsKeys 类型,而不是String 类型。解决方案是使用Keys.items.rawValue 而不是Keys.items 进行索引。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多