Collection types
Although we don't have space here to discuss all the methods that are made available by Array
and Dictionary
types, we should cover, albeit briefly, the most common operations on those objects.
Note
This section provides examples of these most common operations, and it is assumed that they are mostly self-explanatory; elaborations are added where that is possibly not the case.
Arrays
We'll start, as one usually does, with arrays.
Comparing arrays
Arrays, being value types, are compared according to their contents:
let arrA = [1, 2, 3] let arrB = [1, 2, 3] let arrC = [3, 4, 5] arrA == arrB arrA != arrC
Both these comparisons return true
, as one would expect.
Mutating an array
Adding and inserting individual elements or several elements is performed as follows:
var arr = [2, 3, 5, 7] arr.append(11) let tens = [10, 20, 30] arr.append(contentsOf: tens) arr.insert(13, at: 5) arr.insert(40, at: arr.endIndex) arr[6...8] = [17, 19, 23]
Using arrays to create new arrays
These methods...