2016-02-11 23 views
1

我更新到7.2的Xcode後錯誤彈出說「曖昧使用的下標」在下面:曖昧使用標使用[indexPath.section] [indexPath.row]

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 

     /*ERROR - Ambiguous use of subscript*/ 
     cell.textLabel?.text = self.tvArray[indexPath.section][indexPath.row] as? String 

     //..... more code 
} 

誰能告訴我我在執行tvArray時做了什麼錯誤?

的設置:

var tvArray = [] 

override func viewWillAppear(animated: Bool) { 
super.viewWillAppear(true) 

    //..... more code 
    tvArray = [["Airing Today", "On The Air", "Most Popular", "Top Rated"], ["Action & Adventure", "Animation", "Comedy", "Documentary", "Drama", "Family", "Kids", "Mystery", "News", "Reality", "Sci-Fic & Fantasy", "Soap", "Talk", "War & Politics", "Western"]] 
    //..... more code 
} 

回答

5

tvArray = []沒有明確的類型推斷[AnyObject]

告訴編譯器數組的正確類型:Array包含String的數組。
然後它知道數組可以通過索引來下標。

var tvArray = Array<[String]>() 

var tvArray = [[String]]() 

額外的好處:在cellForRowAtIndexPath類型轉換不需要

cell.textLabel?.text = self.tvArray[indexPath.section][indexPath.row] 
+0

謝謝,我想我忘了那部分。奇怪的是編譯器沒有在7.1中抱怨:0 – Mat0

+0

編譯器也在學習;-) – vadian