Allister Bah
Allister Bah

Reputation: 1144

Swift: How to add dictionary arrays to an array?

Can I add dictionary arrays (is this the correct term for a dictionary key holding multiple values?) to an array?

var dictionary = [String: [String]]()
var array = [String]()

var data1:String = "55a"
var data2:String = "95a"
var data3:String = "66"
var data4:String = "25"
var data5:String = "88b"
var data6:String = "#"

dictionary["3"] = [data1, data2, data3, data4, data5, data6]

var data7:String = "#"
var data8:String = "#"
var data9:String = "#"
var data10:String = "#"
var data11:String = "#"
var data12:String = "#"

dictionary["2"] = [data7, data8, data9, data10, data11, data12]

var data13:String = "100"
var data14:String = "101"
var data15:String = "102"
var data16:String = "103"
var data17:String = "104"
var data18:String = "105"

dictionary["1"] = [data13, data14, data15, data16, data17, data18]

I tried this:

array.extend([dictionary["1"], dictionary["2"], dictionary["3"]])

but there was an error "Cannot invoke 'extend' with an argument list of type '([[(String)?])"..

How do I add dictionary["1"], ["2"] & ["3"] accordingly into the array?

Upvotes: 4

Views: 8867

Answers (5)

ABakerSmith
ABakerSmith

Reputation: 22939

If you wanted an array of arrays of Strings, you need to change your array's type to be [[String]], as the other answers said.

But, when getting values out of your dictionary, you shouldn't force unwrap! It may work for this example, but in the future you'll likely get into trouble with:

'fatal error: unexpectedly found nil while unwrapping an Optional value'

You should check to see if a value exists in the dictionary for that key, using optional binding for example:

if let value = dictionary["1"] {
    array.append(value)
}

// ...

Or, you could get all the values from your dictionary into an array like so:

let array = Array(dictionary.values)

If you actually did want an array of Strings, you could use flatMap:

let array = flatMap(dictionary.values) { $0 }

Upvotes: 3

Santu C
Santu C

Reputation: 2654

Your array type declaration is not correct. Please try below one

var array: [[String:[String]] = []

Upvotes: 5

Matteo Piombo
Matteo Piombo

Reputation: 6726

In case you are not interested in the order you might try:

array.extend(flatMap(dictionary.values, {$0}))

If order is important you might build your optionalArrays first:

let optionalArrays = [dictionary["1"], dictionary["2"], dictionary["3"]]
array.extend(flatMap(optionalArrays, {$0 ?? []}))

i.e. your dictionary returns an optional array, this causes the error you reported.

Hope this helps

Upvotes: 3

giorashc
giorashc

Reputation: 13713

Dictionary values are returned as optionals (thus indicating if a value exists for a key) so use the '!' to unwrap the values of each dictionary array (i.e. [dictionary["1"]!)

And as suggested in other answers change your array type as it currently defined as arrays of string rather then an array of dictionaries.

Upvotes: 1

Marius Fanu
Marius Fanu

Reputation: 6669

Your array variable must be an Array of Array with String elements. Also don't forget to unwrap the values of the dictionaries by adding !.

Try this:

var dictionary = [String: [String]]()
var array = [[String]]()


var data1:String = "55a"
var data2:String = "95a"
var data3:String = "66"
var data4:String = "25"
var data5:String = "88b"
var data6:String = "#"

dictionary["3"] = [data1, data2, data3, data4, data5, data6]

var data7:String = "#"
var data8:String = "#"
var data9:String = "#"
var data10:String = "#"
var data11:String = "#"
var data12:String = "#"

dictionary["2"] = [data7, data8, data9, data10, data11, data12]

var data13:String = "100"
var data14:String = "101"
var data15:String = "102"
var data16:String = "103"
var data17:String = "104"
var data18:String = "105"

dictionary["1"] = [data13, data14, data15, data16, data17, data18]
array.extend([dictionary["1"]!, dictionary["2"]!, dictionary["3"]!])

Upvotes: 2

Related Questions