Swift字典,数组为值

las*_*off 12 ios swift

如何声明一个具有数组值的字典?这甚至可能吗?

yus*_*024 18

是

let myDictionary: [String: [Int]] = ["Hello": [1, 2, 3], "World": [4, 5, 6]]
Run Code Online (Sandbox Code Playgroud)

实际上,如果您分配了初始值,则甚至不需要显式类型声明.它可以简单到:

let myDictionary = ["Hello": [1, 2, 3], "World": [4, 5, 6]]
Run Code Online (Sandbox Code Playgroud)

要使用该值:

println(myDictionary["Hello"][0]) // Print 1
println(myDictionary["World"][0]) // Print 4
Run Code Online (Sandbox Code Playgroud)