Mat*_*at0 1 arrays ambiguous ios swift swift2
在我更新到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
}
Run Code Online (Sandbox Code Playgroud)
谁能告诉我在实施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
}
Run Code Online (Sandbox Code Playgroud)
tvArray = []没有显式类型被推断[AnyObject].
告诉编译器数组的正确类型:Array包含数组String.
然后它知道数组可以通过索引下标.
var tvArray = Array<[String]>()
Run Code Online (Sandbox Code Playgroud)
要么
var tvArray = [[String]]()
Run Code Online (Sandbox Code Playgroud)
附加好处:cellForRowAtIndexPath不需要类型铸造
cell.textLabel?.text = self.tvArray[indexPath.section][indexPath.row]
Run Code Online (Sandbox Code Playgroud)