gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 06:06:41 27 4
gpt4 key购买 nike

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

关于ios - 使用 [indexPath.section][indexPath.row] 的下标使用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35343319/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com