Programming in Swift: Fundamentals

Oct 19 2021 · Swift 5.5, iOS 15, Xcode 13

Part 2: Beginning Collections

13. Arrays

Episode complete

Play next episode

About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 12. Challenge: Tuples Next episode: 14. Operating on Arrays

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 13. Arrays

Apple’s Swift Array documentation:

Update Notes: The student materials have been reviewed and are updated as of October 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

In this exercise you learn about one of the most common collection types in Swift, Arrays. But, what exactly is an array? Well, an array lets you store an ordered list to values. Like you see in this grid. Each value store in the array is called an element. Now, arrays can only store values of the same type. Now 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. Arrays are also ordered. Now that means each element in the array has a position called an index. You might remember indexes from working with tuples. Elements and arrays are zero indexed, which means, the index of the first element is zero. The index of the second element is 1 and so on. Now, the position of each element will stay the same unless you change it later. So, when would you want to use an array? Now, arrays are useful when you want to store items in a particular order. Now this can be really handy, if you wanna sort a list of items. So, for example, if you are storing high scores in a game, then you might want the highest score to come first with the next highest score after that and so on. Arrays can also be handy, if you want to be able to get an item from a list at a particular index. For example, you might display a list of pastries in your app. And if a user taps on the third one in the list you 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. So first of all, how do you make an array? Well, let's declare a new constant and we'll name it pastries. (Instructor typing) And then to make it an array, we use a pair of square brackets. And then we can put a comma-separated list of values inside. So cookie, (Instructor typing) cupcake, (Instructor typing) donut, (Instructor typing) and pie. (Instructor typing) So, we're using type inference here 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 you can declare the type with a pair of brackets around the name of the type. Like this. (Instructor typing) So, you've got some pastries, but what if you don't know what you want in your array yet? So, 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. Because 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. So, let's comment this out to start. And I'll copy the first part of this declaration. Then, I'm gonna change the let to var. And I'm gonna set the value to an empty pair of square brackets. And now, because it's a variable, you can use what's known as the append method to add elements to this array. So, you can do this by stating the name of the array you want to operate on than a dot, then the name of the method, append. And then the value you want to add. (Instructor typing) (Instructor typing) Hey, I've just added a cookie, to the empty array. Now, let's add another pastry to the array, say a Danish. (Instructor typing) Now, Swift has append the new element to the end of the array. Now, you don't have to add just single elements to the end of the 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 plus equals operator. (Instructor typing) (Instructor typing) (Instructor typing) So, here you've added another array to the end of the array, you already had. So, you can see over on the right that, cupcake comes after the first two elements you added, cookie and Danish. So again, every time you add an element or set of elements to an existing array, either using append or the plus equals operator, the added elements will appear at the end of the array. Now, it's all well and good to add elements to our arrays, but how do you get them back out? Go learn all of this and more in the next episode. I'll see you there.