2015-11-25 45 views
3

我有一個可擴展的表格,其中包含自定義單元格,當點擊一個可見行時出現或消失。單元格的數據存儲在plist中並聲明爲NSMutableArray。模糊使用下標

我在下面的代碼中得到'模糊使用下標'的錯誤,並希望其他人遇到這種情況並知道修正。 我已經嘗試了所有可能的選擇,對於我必須添加的有限知識。

var cellDescriptors: NSMutableArray! 

func getCellDescriptorForIndexPath(indexPath: NSIndexPath) -> [String: AnyObject] { 
     let indexOfVisibleRow = visibleRowsPerSection[indexPath.section][indexPath.row] 
     let cellDescriptor = cellDescriptors[indexPath.section][indexOfVisibleRow] as! [String: AnyObject] 
     return cellDescriptor 
    } 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     let indexOfTappedRow = visibleRowsPerSection[indexPath.section][indexPath.row] 

     if cellDescriptors[indexPath.section][indexOfTappedRow]["isExpanded"] as! Bool == true { // Ambiguous use of subscript error 
      var shouldExpandAndShowSubRows = false 
      if cellDescriptors[indexPath.section][indexOfTappedRow]["isExpanded"] as! Bool == false { // Ambiguous use of subscript error 
       // In this case the cell should expand. 
       shouldExpandAndShowSubRows = true 
      } 

回答

1

編譯器告訴你它不能確定你下標的對象的類型實際上可以下標。你沒有提供任何有關你稱之爲「單元描述符」的信息,因此很難確定問題是什麼,但是看起來你期望從下標調用indexOfTappedRow查找的任何字典中返回。

這可能是由於使用可可集合像NSArray/NSMutableArrayNSDictionary/NSMutableDictionary,這是AnyObject容器(其中失去鍵入有關正是它的存儲信息)。由於這些免費橋接到Swift數組和字典,很容易將模型(包括容器)更改爲使用特定類型,以便容器可以明確聲明它們是「一組CellDescriptor」(與NSArray的「一些隨機類型的對象(AnyObject)」)。通過這種方式,您不必做一些不雅的事情,比如通過一些容易出錯的字符串來查找屬性,這些字符串可能會在這裏或那裏錯誤輸入(使用as!會使其更糟糕,這要求它是「有效或爆炸」與「對或錯」)。

將Swift的強類型系統視爲可以利用的東西,而不是儘可能避免的東西。事情變得更容易了。

更新評論

在此情況下(因爲PLIST是一個純可可對象圖),你可以擴展你的代碼拿東西在一步一個腳印。那就是:

  1. 獲取部分辭書的數組(我們假定這種結構,因爲它知道你的結構的PLIST但總是有餘地意外...)
  2. 獲取字典該行as? NSDictionary(提示:if let...
  3. 獲得價值爲"isExpanded"關鍵as? NSNumber(提示:if let...
  4. 如果你已經遠遠得到這一點,使用NSNumberboolValue。沒有歧義。
+0

謝謝你的廣泛解釋約書亞。我理解這個問題,但不知道如何解決。我的數組/字典存儲在一個plist中,並描述了每個單元格的屬性,如isExpandable,isExpanded,isVisible,Value,Title等。爲了將此移動到'純粹'Swift,您可以給一些指導/建議以使用特定類型? –

3

肺心病,我假設你現在已經解決了這個不過話說找到了解決辦法,我想我應該把它帶到你的注意:

func getCellDescriptorForIndexPath(indexPath: NSIndexPath) -> [String: AnyObject] { 
    let indexOfVisibleRow = visibleRowsPerSection[indexPath.section][indexPath.row] 
    // HERE IS WHAT YOU WANT: 
    let cellDescriptor = (cellDescriptors[indexPath.section] as! NSMutableArray)[indexOfVisibleRow] as! [String: AnyObject] 
     return cellDescriptor 
    } 
0
if cellDescriptors[indexPath.section][indexOfTappedRow]["isExpanded"] as! Bool == true { 
     var shouldExpandAndShowSubRows = false 
     if cellDescriptors[indexPath.section][indexOfTappedRow]["isExpanded"] as! Bool == false { // Ambiguous use of subscript error 
      // In this case the cell should expand. 
      shouldExpandAndShowSubRows = true 
     } 
} 

應改爲:

if **((cellDescriptors[indexPath.section] as! NSMutableArray)[indexOfTappedRow] as AnyObject)["isExpandable"] as! Bool** == true { 
     var shouldExpandAndShowSubRows = false 
     if ((cellDescriptors[indexPath.section] as! NSMutableArray)[indexOfTappedRow] as AnyObject)["isExpanded"] as! Bool == false { 
      // In this case the cell should expand. 
      shouldExpandAndShowSubRows = true 
     } 
}