1

I am trying to declare a variable that stores an array of arrays. Each array then contains a Dictionary object. I tried a bunch of different possible declarations but the compiler is not happy with any of them.

Any suggestion?

Thanks

Clarification on what I needed:

An array that contains arrays where each of them contains dictionaries.

1 Answer 1

1

You Said

Each array then contains a Dictionary object

that mean that each array will have a single Dictionary so why do you need that extra Array

anyway the following code declare an Array that contain Dictionaries(The code assume that eventually your dictionary will contain strings but you can change that to any type you want)

var myObj  = Array<Dictionary<String,String>>()

var dic1:Dictionary<String,String> = Dictionary<String,String>()
dic1["A"] = "Alpha :A"
dic1["B"] = "Alpha :B"


var dic2:Dictionary<String,String> = Dictionary<String,String>()
dic2["C"] = "Alpha :C"
dic2["D"] = "Alpha :D"

myObj.append(dic1)
myObj.append(dic2)

if you need an array containing arrays of dictionaries it will be almost the same

var myObj  = Array<Array<Dictionary<String,String>>>()
Sign up to request clarification or add additional context in comments.

3 Comments

this works. I ended up making it an optional because of what I need to do so I used: "var cars:[[Dictionary<String, String>]]?"
The compiler seems to accept your solution it seems to be equivalent to Array<Array<Dictionary<String,String>>>() , i still prefer my way maybe because I am coming from java background so it seems natural to me.
@zumzum: You could even go nuts and use var cars: [[[String: String]]] = []. I'd suggest you reconsider making the array optional -- it adds a bit of extra work, and an empty array can have essentially the same meaning as a nil optional value.

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.