【问题标题】:Ambiguous use of subscript using [indexPath.section][indexPath.row]使用 [indexPath.section][indexPath.row] 模糊使用下标
【发布时间】:2016-02-11 15:25:20
【问题描述】:

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

【问题讨论】:

    标签: ios arrays swift swift2 ambiguous


    【解决方案1】:

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

    告诉编译器数组的正确类型:一个Array,包含String的数组。
    然后就知道数组可以下标了。

    var tvArray = Array<[String]>()
    

    var tvArray = [[String]]()
    

    额外的好处:不需要cellForRowAtIndexPath 中的类型转换

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

    【讨论】:

    • 谢谢,我想我忘记了那部分。奇怪的是编译器在 7.1 中没有抱怨:0
    猜你喜欢
    • 2014-10-25
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多