我试图创建一个列表来保存带有部分的tableview的数据。
我想这样使用它:
cell.NameLabel.text = list[indexPath.section][indexPath.row].name
已编辑
我试图简化这个问题,因为英语不是我的主要语言。
让我尝试提出正确的问题:
我想创建一个包含元组数组的字典
像这样:
var myDict = Dictionary<Array<(code: String, type: String)>>()
我想这样访问:
myDict["blue"][0].type
最佳答案
您的示例中的myDict
声明是错误的,因为Dictionary
要求键的类型和值的类型。您应该将其声明为:
var myDic = Dictionary<String, Array<(code: String, type: String)>>()
然后,您可以(几乎)按需使用它:
myDic["one"] = [(code: "a", type: "b")]
myDic["two"] = [(code: "c", type: "d"), (code: "e", type: "f")]
let t = myDic["two"]![0].type
...
注意
!
之后的myDic["two"]
。那是因为通过键访问Dictionary
会返回Optional
,因此您需要先将其打开。实际上,此代码会更好:
if let item: Array<(code: String, type: String)> = myDic["two"] {
let t = item[0].type
...
}