使用 [indexPath.section][indexPath.row] 的下标使用不明确
Ambiguous use of subscript using [indexPath.section][indexPath.row]
我更新到 Xcode 7.2 后弹出一个错误提示 "Ambiguous use of subscript" 在下面:
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
那么它就知道数组可以通过index下标了。
var tvArray = Array<[String]>()
或
var tvArray = [[String]]()
额外好处:不需要 cellForRowAtIndexPath
中的类型转换
cell.textLabel?.text = self.tvArray[indexPath.section][indexPath.row]
我更新到 Xcode 7.2 后弹出一个错误提示 "Ambiguous use of subscript" 在下面:
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
那么它就知道数组可以通过index下标了。
var tvArray = Array<[String]>()
或
var tvArray = [[String]]()
额外好处:不需要 cellForRowAtIndexPath
中的类型转换
cell.textLabel?.text = self.tvArray[indexPath.section][indexPath.row]