使用[indexPath.section] [indexPath.row]模糊地使用下标

时间:2016-02-11 15:25:20

标签: ios arrays swift swift2 ambiguous

在我更新到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
}

1 个答案:

答案 0 :(得分:5)

没有显式类型的

tvArray = []被推断为[AnyObject]

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

var tvArray = Array<[String]>()

var tvArray = [[String]]()

其他好处:不需要cellForRowAtIndexPath中的类型转换

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