SWIFT NOTES - COLLECTION TYPES

Notes about Swift collection types.

Array

Type notation:

Initializes an array:

// full syntax
let a = Array<Element>()

// short & preferred syntax
let a = [Element]()

// shortest syntax when the type inference is present
// `[]` here is the empty array literal with the assistence of type inference
let a: [Element] = []

func dealWith(array: [Element]) {
// ...
}

dealWith(array: [])
// full syntax
let a = Array<Double>(count: 5, repeatedValue: 0.0)

// short & preferred syntax
let a = [String](count: 10, repeatedValue: "default text")
let a1 = [Int](count:3, repeatedValue: 0)
let a2 = [Int]()
let a = a1 + a2
let a = ["Eggs", "Oil"]
let b: [Int] = [] // is equivalent to `let b = [Int]()`

Set

Type notation:

Initializes an set:

// full syntax
let a = Set<Element>()

// shortest syntax when the type inference is present
let a: Set<Int> = [] // same as array

func dealWith(set: Set<Int>) {
// ...
}

dealWith(set: [])
// similar with array, but must be annotated as a Set type explicitly, thus
// force converting the array literal into a set
let a: Set<String> = ["Apple", "iOS", "Mac OS X"]

// annotation can be shortened by type inference
let a: Set = [1, 2, 3]

Dictionary

Type annotation:

Initializes an array:

// full syntax
let a = Dictrionary<Key: Value>()

// short & preferred syntax
let a = [Key: Value]()

// shortest syntax when the type inference is present
// `[:]` here is the empty dictionary literal with the assistence of type inference
let a: [Key: Value] = [:]

func dealWith(array: [Key: Value]) {
// ...
}

dealWith(array: [:])