使用 [indexPath.section][indexPath.row] 的下标使用不明确



在我更新到 Xcode 7.2 后,弹出一个错误,在下面说"下标的模糊使用":

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
}
没有

显式类型的tvArray = []被推断为[AnyObject]

告诉编译器数组的正确类型:包含 String 数组的Array
然后它知道数组可以通过索引下标。

var tvArray = Array<[String]>()

var tvArray = [[String]]()

附加优点:不需要cellForRowAtIndexPath中的类型转换

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

最新更新