2016-09-22 46 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]]] 

現在,您的代碼(有問題提及)將按原樣運行,無需任何更改!

相關問題