Swift Arrays - Modify and Add Elements
In this article, we'll go over the basics of Swift Arrays and explain how to modify and add elements to an array.
The Basics of Swift Arrays
A Swift Array is an ordered collection of values.
Array element can be a string, a number, or even an object. Unlike a Set, arrays don't require unique values, so duplicate elements are allowed.
To declare an array in Swift, specify the data type in square brackets.
For example, here's how you can define an empty array of strings named names:
var names: [String] = []
You can also write it like this, but the Swift documentation recommends using the [Type] style.
var names: Array<String> = Array()
Like other variables, you can also skip the type and initialize it directly with values:
var names = ["Smith", "Williams", "Miller", "Brown"]
If you don't need to modify the array, use let instead of var:
let names = ["Smith", "Williams", "Miller", "Brown"]
Now let's take a closer look at how to use arrays in Swift.
Accessing Array Elements
The individual values in an array are called elements. You can access them by specifying their index.
Array indexes in Swift start at 0, not 1. The first element is index 0, the second is index 1, the third is index 2, and so on.
For example, here's how to access the first and third elements:
let names = ["Smith", "Williams", "Miller", "Brown"]
print(names[0])
print(names[2])
Output:
Smith
Miller
Accessing Elements with Ranges
You can use Swift range operators to access multiple elements at once.
let names = ["Smith", "Williams", "Miller", "Brown"]
print(names[1...2])
print(names[2...])
print(names[...2])
print(names[..<2])
Here's what each operator means:
- 1...2 → indices 1 through 2
- 2... → index 2 through the end
- ...2 → from the beginning through index 2
- ..<2 → from the beginning through index 1
Output:
["Williams", "Miller"]
["Miller", "Brown"]
["Smith", "Williams", "Miller"]
["Smith", "Williams"]
If you write let names2 = names[1...2], the result is an ArraySlice, which still references the original array. That means names2[1] is "Williams", but trying names2[0] will cause an error. If you want a new standalone array, use let names2 = Array(names[1...2]). This creates a new array with indices starting at 0.
Changing Values in an Array
To change a value in a Swift array, assign a new value to the element at the specified index.
For example, if you want to change the second element to "Taylor", you can do it like this:
Since the array is being modified, the variable must be defined with var instead of let.
var names = ["Smith", "Williams", "Miller", "Brown"]
print(names)
names[1] = "Taylor"
print(names)
The output looks like this, with the second element updated to "Taylor".
["Smith", "Williams", "Miller", "Brown"]
["Smith", "Taylor", "Miller", "Brown"]
Adding Elements with append()
There are two main ways to add elements to an array: appending to the end, or inserting at a specific index.
To append to the end, use the append() method.
Let's define an empty array and use append() to add three elements to it.
var names: [String] = []
names.append("Smith")
print(names)
names.append("Williams")
print(names)
names.append("Miller")
print(names)
The output shows that each value is added to the end of the array in order.
["Smith"]
["Smith", "Williams"]
["Smith", "Williams", "Miller"]
Combining Arrays with append()
You can also use the append() method to add the elements of another array to the end of an array.
In that case, use it like append(contentsOf: anotherArray).
For example, if you define another array called names2 and want to add its elements to names, you can do it like this.
var names: [String] = []
var names2 = ["Miller", "Brown"]
names.append("Smith")
print(names)
names.append(contentsOf: names2)
print(names)
The output shows that the elements of names2 have been added.
["Smith"]
["Smith", "Miller", "Brown"]
Inserting Elements with insert()
To add an element to a specific position in a Swift array, you can use the insert() method.
The syntax looks like this: insert(newElement, at: index)
For example, if you want to insert a new value at the second position, you can do it like this.
var names = ["Smith", "Williams", "Miller", "Brown"]
print(names)
names.insert("Anderson", at: 1)
print(names)
The output shows that "Anderson" is inserted at the second position, and the remaining elements are shifted back.
["Smith", "Williams", "Miller", "Brown"]
["Smith", "Anderson", "Williams", "Miller", "Brown"]
That's it for the basics of Swift Arrays and how to modify and add elements.