Group Group Group Group Group Group Group Group Group

Connect several dictionaries

I try to learn Swift. I found a piece of code that when I print it gives that to the console (I simplified here)

var test: [[String: Any]] = [[“key1”: 1.1, “key2”: 2.1, “key3”: “value3.1”]] [[“key1”: “value1.2”, “key2”: “value2.2”, “key3”: “value3.3”]]

I looked at the documentation

https://docs.swift.org/swift-book/LanguageGuide/CollectionTypes.html

I could not find anything like this.

  • What is it? It seems to me like an array of dictionaries? Where could I find more about it? (I could not find it in the documentation)

  • What is the right way to connect the two dictionaries? In the console there is nothing between ]] and [[. I tried to put a coma but Playground gives the error “Expected pattern”. I suppose that something is needed to connect the two dictionaries?

@narcis Do you still have issues with this?

Hi @narcis,
I did not quite understand your question.

var test: [[String:Any]]
is indeed an array of dictionaries, in fact if you use this code, it would be clearer to you

typealias CustomDict = [String:Any]
var test: [CustomDict] = []

so basically each element in this array has to be a CustomDict type, which is basically a dictionary.

If you want to know how to read this, you have to have two loops, one outer to iterate through the elements in the array and the second to iterate through each key value pairs.

If it is anything else, then you might need to reiterate your question.

cheers,

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.