我正在尝试创建一个列表来保存包含部分的表格 View 的数据。
我想这样使用它:
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
...
}
关于ios - Swift - 带有元组数组的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29831036/