【发布时间】:2015-10-22 09:08:01
【问题描述】:
我将字典显示为 UITableView。我在 Objective-c 中很容易做到这一点。但现在很快,我无法从字典数组中获取值。当我尝试在tableView:cellForRowAtIndexPath 中获取值时,预编译器会抛出类似
无法转换“AnyObject?!”类型的值到指定类型'数组'
或者像下面这个例子一样
无法转换“AnyObject?!”类型的值idnex 类型为“int”
var cellArray = []
override func viewDidLoad() {
cellArray = [
[
"section" : "Melody",
"rows" :
[
[
"title" : "Wi-Fi / Pairing",
"icon" : "",
"action" : "actionConnectSpeaker"
],
[
"title" : "Sound",
"icon" : "",
"action" : "actionConnectSpeaker"
]
]
],
[
"section" : "Music",
"rows" :
[
[
"title" : "Add a new account",
"icon" : "",
"action" : "actionAddAccount"
]
]
]
]
}
...
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: nil)
cell.textLabel?.text = cellArray[indexPath.section]["rows"][indexPath.row]["title"]
return cell
}
我的结论是我们不能这样做......我找到任何例子。我可能对 Swift 中的类型不太了解...
【问题讨论】:
标签: arrays swift dictionary swift2