在我更新到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
}
答案 0 :(得分:5)
tvArray = []
被推断为[AnyObject]
。
告诉编译器数组的正确类型:包含Array
数组的String
然后它知道数组可以通过索引下载。
var tvArray = Array<[String]>()
或
var tvArray = [[String]]()
其他好处:不需要cellForRowAtIndexPath
中的类型转换
cell.textLabel?.text = self.tvArray[indexPath.section][indexPath.row]