Arrays - RaduG/swift_learning GitHub Wiki

Create array with default values

var arr = Array(repeating: 0, count: 3) // arr == [0, 0, 0]

Array length

let len = arr.count

Array append

Of a single element:

arr.append(1)

Of another array:

arr += [10, 20]

Changing array values

Of a single element:

arr[1] = 100

Of a slice:

arr[1...3] = [1, 2, 3]

If the right hand side argument has a different length:

// will remove element at index 3
arr[1...3] = [1, 2]
// will add a new element at position 4 
arr[1...3] = [1, 2, 3, 4] 

Inserting an element

arr.insert(100, at: 3)