Array Manipulation In Swift

Become a Subscriber

Note: Video is part of a Swift beginner series by MadApper (a.k.a. Paul Napier)

The last section covered arrays. Now we are going to dive into array manipulation. Before we dive in to array methods, we should talk about array mutability. Mutability means you can change a part of the variable. Immutable variables cannot be changed after it has been declared. In Swift, all variables are mutable. Constants are always immutable. There are four ways to manipulate an array: appending, inserting, removing, replacing.

For our examples, we will use the variable array:

var array = ["a", "b" ,"c", "d", "e"]

Appending is the process of adding an element to the end of an array. This can be done with a method. We will cover methods in greater detail later.

To append an element to the end of our array variable, we will use the append method:

array.append("f")
//array now equals ["a", "b" ,"c", "d", "e", "f"]

NOTE: The video shows an alternative method of appending an array object using the addition assignment operator (+=). THIS IS NO LONGER SUPPORTED IN SWIFT.

What if you want to add an element into the array, but do not want to append it to the end of the array? This is where the insert method comes into play. Let’s insert an object at index 0:

array.insert("0", atIndex: 0)
// array now equals "[0, a, b, c, d, e, f]"

Let’s move on to replacing parts of the array. We can decide which elements we want to replace. We can replace one or a range of objects in the array. In Swift, ranges are declared using the start index and the end index and whether or not it is inclusive or exclusive of the end index.

var rangeExclsive = 1..<3
// range from 1 to 3, excludes 3

var rangeInclusive = 1...3
// range from 1 to 3, includes 3

array[rangeExclsive]
// returns ["a", "b"]

array[rangeInclusive]
// returns ["a", "b", "c"]

NOTE: The video shows a deprecated syntax for declaring exclusive ranges. startIndex..endIndex is no longer supported in Swift. Use startIndex..<endindex instead.

Inclusive vs. exclusive arrays is an important distinction when replacing objects in arrays. Let’s begin by simply replacing one object with another using the assignment operator (=):

array[0] = "1"

The above example assigns the array object at index 0 to the string “1”. array is now [“1”, “a”, “b”, “c”, “d”, “e”, “f”]. No we are going to use the assignment operator to replace a range of objects at different indexes

array[1..<4] = ["2","3","4"]

Here we assign the array objects at the range of indexes to the corresponding values in our array of strings. Our exclusive contains three values because our range excludes 4. Our assignment array also contains three values. In this case our array object at index 1 is being replaced by the first object in our assignment array, “2”. The array object at index 2 is being replaced by “3” and so on. Array is now [“1”, “2”, “3”, “4”, “d”, “e”, “f”]. We can do the exact same thing using the replaceRange method:

array.replaceRange(5...6, with: ["5","6"])

Our variable array now equals [“1”, “2”, “3”, “4”, “d”, “5”, “6”]. Finally let’s cover removing objects from arrays.There are three ways. We can use the removeLast method, which removes the last element of the array. There is also the removeAtIndex method, which takes the index you want to remove as a parameter. Finally, you can use the removeAll method, which removes all of the objects from the array. Let’s look at examples:

var array = ["1", "2", "3", "4", "d", "5", "6", "g"]

array.removeLast()
// array now equals ["1", "2", "3", "4", "d", "5", "6"]

array.removeAtIndex(4)
// array now equals ["1", "2", "3", "4","5", "6"]

array.removeAll(keepCapacity: false)
// array now has zero elements. 

The keepCapacity = false removes all of the elements from the array, and by default clears the underlying storage buffer.