Leave a rating/review
Notes: 13. Arrays
Apple’s Swift Array documentation: https://developer.apple.com/documentation/swift/array
Update Notes: The student materials have been reviewed and are updated as of October 2021.
In this exercise, you’ll learn about one of the most common collection types in Swift: arrays.
What exactly is an array?
That means you can create an array of Ints like 1, 2, 3, or an array of Strings like Ozma, Felix, and Christopher Walken, but you can’t create an array of Ints and Strings, like 1, Garfield, 3.
Elements in arrays are zero-indexed, which means the index of the first element is 0, the index of the second element is 1 and so on. The position of each element will stay the same, unless you change it later.
So when would you want to use an array?
This can be handy if you want to sort a list of items. For example, if you were storing high scores in a game, then you might want the high score to come first, with the next-highest score after that, and so on.
For example, you might display a list of pastries in your app, and if the user taps on the third one in the list, want to be able to quickly find that pastry.
Now that you know what arrays are, and why you might want to use them, let’s see how to use them in Swift.
As with the previous parts, you can create your own playground, or use the starter playground we’ve made for you!
First of all, how do you make an array?
Declare a new constant, and name it pastries.
let pastries =
Then to make it an array, use a pair of square brackets
let pastries = []
and then put a comma-separated list of values inside
let pastries = ["cookie", "cupcake", "donut", "pie"]
- We’re using type inference, again. We have a bunch of Strings wrapped in square brackets, so the compiler knows that this is an array of Strings!
But, as always, you can be explicit and declare the type with a pair of brackets around the name of the type
let pastries: [String] = ["cookie", "cupcake", "donut", "pie"]
So we have some pastries, but what if you don’t know what you want in your Array yet?
With a String or an Int you might declare a constant without setting a value.
With an Array, you can actually set the value to be an empty array that you’ll fill up later! But you can only do that with a variable.
Adding elements to an array is a form of mutation, and constants can’t be mutated or changed. So you’ll have to use a variable instead.
- To do that, copy the first part of this declaration and comment out the first declaration.
Then change the let to var and set the value to empty pair of square brackets.
var pastries: [String] = []
Now, because it’s a variable, you can use what’s known as the append method to add elements to the array. You do this by stating the name of the array you want to operate on, then a dot, then the name of the method (append), and then the value you want to add:
pastries.append("cookie")
I’ve just added a cookie to the empty array!
Now, if you add another pastry to the array, say, a danish:
pastries.append("danish")
Then Swift will append your new element to the end of the array.
Now, you don’t have to add just single elements to an array. You can add multiple elements all at once to your array. You already know that a collection of elements is an array, and you can add one array to another. To do that, you use the += operator.
pastries += ["cupcake", "donut", "pie", "brownie"]
- Here we’ve added another array to the end of the array we already had.
You can see over on the right that “cupcake” comes after the first two elements you added, “cookie” and “danish”.
Again, every time you add an element or set of elements to an existing array either using append or the += operator, the added elements will appear at the end of the array.
Now, it’s all well and good to add elements to arrays — but how do you get them back out? You’ll learn all this, and more, in the next episode. See you there!