如何在swift中向字典添加字典

时间:2016-02-17 09:58:45

标签: swift

如何在数据后面的数组中初始化和添加值

(
    [0] => Array
        (
            [name] => First element
            [foo] => bar
        )

    [1] => Array
        (
            [name] => Second element
            [foo] => bar2
        )

)

1 个答案:

答案 0 :(得分:13)

包含词典的那个数组,你在追求它吗?

let myArray = [["name": "First element", "foo": "bar"], ["name": "Second element", "foo": bar2]]

//在循环中编辑添加项目:

var myArray = [[String: String]]()
for i in 0..<10 {

    let dict = ["name": "Item\(i)", "foo": "bar\(i)"]
    myArray.append(dict)
}
print("\(myArray)")