2016-09-22 48 views
0

我正在通过这appcoda blog并遇到一个函数,我必须得到可见行的索引。我正在学习和实施Swift 3/Xcode 8。对于以下功能,我得到No subscript members错误。'NSFastEnumerator.Element'(又名'Any')没有下标成员

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”在这里?

+0

你确定有**两个循环吗? – vadian

+0

@vadian:是的,有两个for循环。请参考链接博客中的功能。 –

+1

我不明白,** Swift **教程仍然建议不相关的集合类型,如'MSMutableArray'和丑陋的老式C风格循环。 Swift中有更好更有效的方法。 – vadian

回答

3

你需要指定数组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) 
} 
+0

得到警告,我仍然得到模糊的编译器错误引用成员下标'if''Cast from'NSMutableArray!'到不相关的类型'[[[String:Any]]]'总是失败 –

+0

@DeepakThakur检查编辑的答案。 –

0

试试这个:

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

而且

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

1.警告:种姓'NSMutableArray!'不相关的类型'[[String:AnyObject]]'总是失败2.错误:对成员'下标'的歧义引用 –

+0

如何声明'cellDescriptors'? –

+0

var cellDescriptors:NSMutableArray! –

0

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

一件事是,创建你cellDescriptors为特定迅速阵列如下。

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

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

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

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

相关问题