Declare a two-dimensional array that contains a Swift dictionary?

1

I need to create a two-dimensional array that contains a dictionary. I've declared it this way but it throws me wrong

var miArray = [String: AnyObject][ ][ ]

To fill it in, it would be:

miArray[columna][linea] = ["llave1":"dato1", "llave2":"dato2"]

How many ways or forms would there be to declare it?

    
asked by Popularfan 27.03.2018 в 14:04
source

1 answer

1

You must declare the array as:

var miArray: Array< Array< Dictionary<String, String> > >

or shorter way like:

var miArray: [ [ [String: String] ] ]

To initialize it you use an expression equal to the previous one:

miArray = [ [ ["llave1":"dato1", "llave2":"dato2"] ] ]

or you can upload items one by one:

miArray.append( [] )
miArray[0].append( ["llave1":"dato1", "llave2":"dato2"] )

Then you can access an element of the array in this way:

let d = miArray[0][0]    // d es un diccionario con valor ["llave1":"dato1", "llave2":"dato2"]
    
answered by 27.03.2018 / 14:21
source