Initialising empty arrays of dictionaries in Swift

ArraysDictionarySwift

Arrays Problem Overview


I'm trying to wrap my head around initialising empty arrays in Swift.

For an array of strings it's pretty straight forward :

var myStringArray: String[] = []
myStringArray += "a"
myStringArray += "b"

-> ["a", "b"]

and for integers

var myIntArray: Int[] = []
myIntArray += 1
myIntArray += 2

-> [1, 2]

it also works for other types of object such as NSImage objects :

let path = "/Library/Application Support/Apple/iChat Icons/Flags/"
let image1 = NSImage(byReferencingFile: path + "Brazil.png")
let image2 = NSImage(byReferencingFile: path + "Chile.png")

var myImageArray: NSImage[] = []
myImageArray += image1
myImageArray += image2

-> [<NSImage 0x7fe371c199f0 ...>, <NSImage 0x7fe371f39ea0 ...>]

However I can't work out the syntax to initialise an empty array of Dictionaries.

I know you can have an array of Dictionaries because initialising with an initial value works :

let myDict1 = ["someKey":"someValue"]
let myDict2 = ["anotherKey":"anotherValue"]

var myDictArray = [myDict1]
myDictArray += myDict2

-> [["someKey": "someValue"], ["anotherKey": "anotherValue"]]

This however (which you'd expect the syntax to be) fails :

var myNewDictArray: Dictionary[] = []

with the error Cannot convert the expression's type 'Dictionary[]' to type 'Hashable'

So the question is what is the correct way to initialise a empty array of Dictionary Items and why doesn't this syntax var myNewDictArray: Dictionary[] = [] work?

Arrays Solutions


Solution 1 - Arrays

You need to give types to the dictionaries:

var myNewDictArray: [Dictionary<String, Int>] = []

or

var myNewDictArray = [Dictionary<String, Int>]()

Edit: You can also use the shorter syntax:

var myNewDictArray: [[String:Int]] = []

or

var myNewDictArray = [[String:Int]]()

Solution 2 - Arrays

This will create an empty, immutable dictionary:

let dictionary = [ : ]

And if you want a mutable one:

var dictionary = [ : ]

Both of these dictionaries default to Dictionary<String?, AnyObject?>.

Solution 3 - Arrays

The reason this isn't working:

var myNewDictArray: Dictionary[] = []

is that you need to provide types for the keys and values of a dictionary when you define it. Each of these lines will create you an empty array of dictionaries with string keys and string values:

var dictArray1: Dictionary<String, String>[] = Dictionary<String, String>[]()
var dictArray2: Dictionary<String, String>[] = []
var dictArray3 = Dictionary<String, String>[]()

Solution 4 - Arrays

You can no longer use element concatenation.

class Image {}
Image i1
Image i2

var x = [Image]()

x += i1 // will not work (adding an element is ambiguous)
x += [i1] // this will work (concatenate an array to an array with the same elements)

x += [i1, i2] // also good

Solution 5 - Arrays

var yourArrayname = [String]() // String or anyOther according to need

Solution 6 - Arrays

You can use this if u want to use swift 2.3!

let path = "/Library/Application Support/Apple/iChat Icons/Flags/"
let image1 = UIImage(contentsOfFile: readPath + "Brazil.png")
let image2 = UIImage(contentsOfFile: readPath + "Chile.png")

var myImageArray = [UIImage]()
myImageArray.append(image1)
myImageArray.append(image2)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondwknsView Question on Stackoverflow
Solution 1 - ArraysdrewagView Answer on Stackoverflow
Solution 2 - ArraysErikView Answer on Stackoverflow
Solution 3 - ArraysNate CookView Answer on Stackoverflow
Solution 4 - ArraysCPDView Answer on Stackoverflow
Solution 5 - ArraysPrashant chaudharyView Answer on Stackoverflow
Solution 6 - ArraysAlejandro ViquezView Answer on Stackoverflow