How to create an array of dictionaries?

new to programming!

I’m trying to create an array of dictionaries inside a struct in Swift like so:

  • Fill an array with strings from a txt file
  • Swift dictionary bug?
  • Swift: How to extract image filename from an array of UIImage
  • How to Properly Declare Array of Custom Objects in Swift?
  • Wrong information displayed on SecondViewController when table row clicked
  • Key-Value Observe in Swift not showing insertions and removals in arrays
  • var dictionaryA = [
        "a": "1",
        "b": "2",
        "c": "3",
        ]
    var dictionaryB = [
        "a": "4",
        "b": "5",
        "c": "6",
        ]
    var myArray = [[ : ]]
    myArray.append(dictionaryA)
    myArray.append(dictionaryB)
    

    This works fine in a playground, but when I put it into an Xcode project, inside a struct, the lines with the append function produce the error “Expected declaration”.

    I’ve also tried using the += operator with the same result.

    How can I successfully construct this array inside the struct?

    3 Solutions Collect From Internet About “How to create an array of dictionaries?”

    From your error Expected declaration, I assume you are doing like:

    struct Foo {
        var dictionaryA = [
            "a": "1",
            "b": "2",
            "c": "3",
        ]
        var dictionaryB = [
            "a": "4",
            "b": "5",
            "c": "6",
        ]
        var myArray = [[ : ]]
        myArray.append(dictionaryA) // < [!] Expected declaration
        myArray.append(dictionaryB)
    }
    

    This is because you can place only “declarations” in the struct body, and myArray.append(dictionaryA) is not a declaration.

    You should do that somewhere else, for example in the initializer. The following code compiles.

    struct Foo {
        var dictionaryA = [
            "a": "1",
            "b": "2",
            "c": "3",
        ]
        var dictionaryB = [
            "a": "4",
            "b": "5",
            "c": "6",
        ]
        var myArray = [[ : ]]
    
        init() {
            myArray.append(dictionaryA)
            myArray.append(dictionaryB)
        }
    }
    

    But as @AirspeedVelocity mentioned, you should provides more information about myArray, or myArray would be Array<NSDictionary> which I think you don’t expect.

    Anyway, the correct solution would vary depending on what you really trying to do:

    Maybe or maybe not, what you want is something like:

    struct Foo {
        static var dictionaryA = [
            "a": "1",
            "b": "2",
            "c": "3",
        ]
        static var dictionaryB = [
            "a": "4",
            "b": "5",
            "c": "6",
        ]
    
        var myArray = [dictionaryA, dictionaryB]
    }
    

    But, I don’t know, why don’t you just:

    struct Foo {
    
        var myArray = [
            [
                "a": "1",
                "b": "2",
                "c": "3",
            ],
            [
                "a": "4",
                "b": "5",
                "c": "6",
            ]
        ]
    }
    

    The problem lies with this line:

    var myArray = [[ : ]]
    

    You need to tell Swift what type myArray is – [[:]] isn’t enough information.

    You can either do it the explicit way:

    var myArray: [[String:String]] = [[ : ]]
    

    Or, if practical, implicitly using the first or both values you plan to put in:

    var myArray = [dictionaryA]
    var myArray = [dictionaryA,dictionaryB]
    

    (as an alternative to the explicit empty version, you can also write var myArray = [[String:String]](), which is shorthand for var myArray = Array<Dictionary<String,String>>())

    Or you can use an array of tuples that´s even easier, like this:

    var myArray:[(a:String,b:String,c:String)] = []
    

    And append any element you need later:

    self.myArray.append((a:"A",b:"B",c:"c"))
    

    And to use them just:

    self.myArray[index].a
    self.myArray[index].b
    self.myArray[index].c