到目前为止我有这个声明:
var meals : [String : Array<String>]
meals = ["Option1": ["01-01-2015","05-02-2015"],
"Option2": ["05-01-2015","10-04-2015"],
"Option3": ["03-02-2015", "07-07-2015"]]
我想做类似的事情,但我不知道如何声明meals
变量:
meals = ["Option1": ["01-01-2015","05-02-2015"], 2
"Option2": ["05-01-2015","10-04-2015"], 2
"Option3": ["03-02-2015", "07-07-2015", "09-08-2015"], 3]
我想将第三个参数添加到字典数组中,即整数,因此每个OptionX
必须有2个参数:日期数组和整数。
我知道字典必须是Key-Value,所以我想知道如何为每个数组添加3个元素。我想我必须创建数组数组?
答案 0 :(得分:3)
您可以使用元组,我会明确说明字典类型以使事情更清晰:
let meals: [String: ([String], Int)] = ["Option1": (["01-01-2015","05-02-2015"], 2),
"Option2": (["05-01-2015","10-04-2015"], 2),
"Option3": (["03-02-2015", "07-07-2015", "09-08-2015"], 3)]
if let option1 = meals["Option1"] {
println(option1.0) // Prints: "[01-01-2015, 05-02-2015]"
println(option1.1) // Prints: "2"
}
或者,您可以使用结构:
struct MealOption {
// More descriptive variable names would be a good idea here...
let a: [String]
let b: Int
}
let meals: [String: MealOption] = ["Option1": MealOption(a: ["01-01-2015","05-02-2015"], b: 2)]
if let option1 = meals["Option1"] {
println(option1.a) // Prints: "[01-01-2015, 05-02-2015]"
println(option1.b) // Prints: "2"
}