【发布时间】:2015-01-05 13:06:06
【问题描述】:
我对 Swift 很陌生。我有一个表视图控制器,我在其中声明了以下内容:
var parts = [String:[String]]() //Key: String, Value: Array of Strings
var partsSectionTitles = [String]()
在我的 viewDidLoad 函数中,我有:
parts = [
"Part 1" : ["1", "2", "3"],
"Part 2" : ["1", "2"],
"Part 3" : ["1"]
]
//Create an array of the keys in the parts dictionary
partsSectionTitles = [String](parts.keys)
在我的 cellForRowAtIndexPath 函数中,我有:
let cell = tableView.dequeueReusableCellWithIdentifier("TableCell", forIndexPath: indexPath) as UITableViewCell
var sectionTitle: String = partsSectionTitles[indexPath.section]
var secTitles = parts.values.array[sectionTitle]
cell.textLabel.text = secTitles[indexPath.row]
我正在尝试创建一个数组 secTitles,它由对应于键 sectionTitle 的部件字典中的值组成。但是,我收到此错误消息:
'String' 不能转换为 'Int'
我能够在 Objective-C 中做到这一点:
NSString *sectionTitle = [partsSectionTitles objectAtIndex:indexPath.section];
NSArray *secTitles = [parts objectForKey:sectionTitle];
另外,我想知道我以后是否能够添加/删除数组和字典中的值。换句话说,它们是可变的吗?我读过一些文章说 Swift 数组和字典实际上不是可变的。我只是想知道是否有人可以证实这一点。提前感谢您的回复。
【问题讨论】:
标签: ios arrays uitableview dictionary swift