14 Useful Array operators in Swift

Rashad Shirizada
4 min readJun 1, 2022

Arrays allow you to aggregate a large number of values into a single collection and then access those values based on where they are in the collection. Swift utilizes type inference to determine the type of data in an array.

Array declaration:

var arr = [2,3,5] 

var arr: [Int] = []
var arr = Array(repeating: 8, count: 3)

1. Adding to the Array

You can add to an array’s end or insert a value at a specific index. The append command or the shorthand += operator can be used to append, as demonstrated below:

arr.append(5)
arr += [5]

For this task, those two statements are functionally equal. One thing to keep in mind is that in order to use the += operator, you must have an array on the righthand side, even if it just contains one value. However, if you wish to add more than one item to the array, you just add to that array literal, thus you could add more values to the array like this:

arr += [9, 8, 7, 6]
[5, 5, 9, 8, 7, 6] are now in the (now awkwardly called) arr (since we added two 5s to it earlier).

The insert command can also be used to insert a value in an arbitrary location, such as this:

arr.insert(92, at: 2)
//arr now is [5, 5, 92, 9, 8, 7, 6]

2. Removing from the Array

--

--