6

How to initialize and add values to array following data

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

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

)
1
  • Arrays are delimited by []. Dictionaries are also delimited by the same characters but there are colons : to separate the keys from the values. Try it out and post your results in the question. Commented Feb 17, 2016 at 10:01

1 Answer 1

17

That's array which contains dictionaries, Is it what you are after?

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

// Edited add items in loop:

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

    let dict = ["name": "Item\(i)", "foo": "bar\(i)"]
    myArray.append(dict)
}
print("\(myArray)")
Sign up to request clarification or add additional context in comments.

2 Comments

Yes but that will be done in loop so I can't create whole array at once, and I have to initialize it first
Thank you i had double brackets missing :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.