【问题标题】:issue on Ambiguous reference to member 'subscript' swift3关于对成员“下标”swift3 的模糊引用的问题
【发布时间】:2016-09-21 06:12:02
【问题描述】:

我已将 swift 2.3 升级到 swift 3,但出现此错误

对成员 'subscript' swift3 的模糊引用

这里是代码

var cellDescriptors: [[String:Any]]!

    func loadCellDescriptors() {
    if let path = Bundle.main.path(forResource: "ProfileDescriptor", ofType: "plist") {
        cellDescriptors = NSMutableArray(contentsOfFile: path)
        getIndicesOfVisibleRows()



        tableView.reloadData()
    }
}


if cellDescriptors[indexPath.section][indexOfTappedRow]["isExpanded"] as! Bool == false {
            shouldExpandAndShowSubRows = true
        }

我在这条线上遇到了错误

if cellDescriptors[indexPath.section][indexOfTappedRow]["isExpanded"] as! Bool == false

给我任何建议,我该如何解决这个问题

【问题讨论】:

  • 感谢回复,但我已经尝试过了,但它不起作用
  • 未解决得到错误
  • 是但不工作
  • NSArray(contentsOfFile: Bundle.main.path(forResource: "CellDescriptor", ofType: "plist")!) as? [[[String: Any]]] ?? [] 是正确的
  • 感谢您的帮助,它正在工作

标签: swift swift3 ios10 xcode8


【解决方案1】:

如果不告诉编译器中间类型,就不能在 Swift 3 中使用订阅链

let section = cellDescriptors[indexPath.section] as! [Any]
let rowItem = section[indexOfTappedRow] as! [String:Any]
if rowItem["isExpanded"] as! Bool == false {
        shouldExpandAndShowSubRows = true
}

和往常一样,除非你别无选择,否则永远不要在 Swift 中使用可变 Foundation 集合类型 NSMutableArray/Dictionary。它们无法桥接到 Swift 并且缺少类型信息。

【讨论】:

  • 感谢我声明 let section = cellDescriptors[indexPath.section] 为! [Any] 我收到此错误 Cast from '[String : Any]' to unrelated type '[Any]' 总是失败
  • 那么你的链接是错误的。根据您的代码,cellDescriptors 应该包含一个数组,因为indexOfTappedRow 似乎是一个整数。 [String:Any] 键下标 (String)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-01
相关资源
最近更新 更多