【问题标题】:'NSFastEnumerator.Element' (aka 'Any') has no subscript members“NSFastEnumerator.Element”(又名“Any”)没有下标成员
【发布时间】:2016-09-22 13:42:21
【问题描述】:

我正在浏览这个appcoda blog 并遇到了一个函数,我必须在其中获取可见行的索引。我正在学习和实施Swift 3 / Xcode 8。以下函数出现No subscript members 错误。

func getIndicesOfVisibleRows() {
    visibleRowsPerSection.removeAll()

    for currentSectionCells in cellDescriptors {
        var visibleRows = [Int]()

        for row in 0...((currentSectionCells as! [[String: AnyObject]]).count - 1) {
            if currentSectionCells[row]["isVisible"] as! Bool == true { //Get compile time error here
                visibleRows.append(row)
            }
        }

        visibleRowsPerSection.append(visibleRows)
    }
}

如何在此处获取 currentSectionCells 数组的对象,其键对象为“isVisible”?

【问题讨论】:

  • 你确定有 两个 for 循环吗?
  • @vadian:是的,有两个 for 循环。请参考链接博客中的功能。
  • 我无法理解 Swift 教程仍然建议不相关的集合类型,例如 MSMutableArray 和丑陋的老式 C 样式循环。 Swift 中有很多更好、更有效的方法。

标签: ios arrays swift swift3


【解决方案1】:

您需要像这样指定数组的类型cellDescriptors[[[String:Any]]]

for currentSectionCells in cellDescriptors.objectEnumerator().allObjects as! [[[String:Any]]]{
    var visibleRows = [Int]()

    for row in 0..<currentSectionCells.count {
        if currentSectionCells[row]["isVisible"] as! Bool == true { 
            visibleRows.append(row)
        }
    }
    visibleRowsPerSection.append(visibleRows)
}

【讨论】:

  • 收到警告:从 'NSMutableArray!' 投射到不相关的类型 '[[[String : Any]]]' 总是失败
【解决方案2】:

试试这个:

for currentSectionCells in cellDescriptors as! [[String: AnyObject]] {

for row in 0...currentSectionCells.count - 1 {

【讨论】:

  • 1.警告:“NSMutableArray!”的种姓到不相关的类型 '[[String: AnyObject]]' 总是失败 2. 错误:对成员 'subscript' 的模糊引用
  • cellDescriptors 是如何声明的?
  • var cellDescriptors: NSMutableArray!
  • 试试:var cellDescriptors:[[String: AnyObject]]!for currentSectionCells in cellDescriptors{
  • 我不能用这个声明加载数组。我正在使用 cellDescriptors = NSMutableArray(contentsOfFile: path);加载数组。我仍然在“if”条件下得到对成员“下标”的模棱两可的编译器错误引用
【解决方案3】:

func getIndicesOfVisibleRows() { visibleRowsPerSection.removeAll()

    for currentSectionCells in cellDescriptors {
        print(currentSectionCells)
        var visibleRows = [Int]()

        for row in 0...((currentSectionCells as AnyObject).count - 1) {

            if (currentSectionCells as AnyObject).objectAt(row)["isVisible"] as! Bool == true {
                visibleRows.append(row)
            }
        }

        visibleRowsPerSection.append(visibleRows)
    }
}

【讨论】:

    【解决方案4】:

    您可以做的另一件事是,将您的 cellDescriptors 创建为特定的 swift 数组,如下所示。

    var cellDescriptors: [[[String: Any]]]!  
    

    并从 .plist 文件中加载您的 cellDescriptor,如下所示。

    cellDescriptors = NSMutableArray(contentsOfFile: path)! as NSArray as! [[[String: Any]]]  
    

    现在,您的代码(在问题中提到)将按原样运行,无需任何更改!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-06
      • 1970-01-01
      • 1970-01-01
      • 2017-10-10
      相关资源
      最近更新 更多