Programming in Swift: Fundamentals

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

Part 2: Beginning Collections

14. Operating on 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: 13. Arrays Next episode: 15. Challenge: 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: 14. Operating on Arrays

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.

So you've learned how to create arrays and how to append elements to them using the append method and the plus equals operator. So you know how to get things into arrays but how do you get them back out? You learn that tuples are zero indexed, where the first element in a tuple is index zero. The next element in a tuple is index one. And so on. And arrays are also zero indexed. So the first element in an array is at index zero. The next element at index one and so forth. Now to get an element at a specific index you use what's known as sub scripting. Now subscripting looks like this. So you have the name of the array and then the index of the element you want in brackets and that's cookie. So you may wonder what happens if there's nothing at an index that you try to subscript into. Well, let's try it. So your array has six elements in it. So let's see what happens if you try to get something at index 13 Whoa that looks scary. But here's the real error message that you need way up here. Index out of range. The range of an array is the span of the indexes in your array. For your six element array up here, it's zero to five. I'll comment that out. So trying to get something from an index that doesn't exist, will give you nothing but errors. Now, ranges can also refer to a subset of the elements in an array. So say, you know for sure there are elements that indexes one through three of an array, and you want to grab that subset of elements and keep a reference to them in a new array. To access a range of elements, simply specify a range inside the brackets to represent the indexes you want. There's a Danish cupcake and donut as expected. This actually creates what's known as an array slice, which is slightly different than an array. It's a stored reference to a part of an array. That means if you tried to get index zero of first three, you'd get an error again because the slice only has references to indexes one through three. I'll comment that out as well. To make a shiny new zero index array, you just need to use slightly different syntax up here. So this takes the small slice of the array and cast it to a brand new array, which then results in an array that does start at index zero, which you can see here. Now, I want to go back for a moment and revisit that append method. Arrays are a special kind of data type in Swift that have things called methods that you can use to perform certain operations on the array. You can also pull up some data about the array as well. You use the append method to add a new element to the array like this. So if you wanted to remove every element from the array, there's a method called remove all to do that. And now your array is empty, but you worked so hard to make all those pastries and we need elements in our arrays to keep exploring. So I'm going to comment that out as well. All right, so your pastries are back. Hmm, what else can we do with them? Well, one piece of data an array has is a Boolean value named is empty, which tells you if the array is empty or not. You can access this piece of data also known as a property with the name of the array, followed by a dot and then follow up with the name of the piece of data you want to look at, or the property, in this case is empty. And we can see that's false in the sidebar over there. You can also use the count property to find out how many pastries you have, And we have seven and you can also find out what the first element in the array is with the first property. And it's our cookie. Now, if you option+click on first, you'll see it's actually an optional. And if you think about that for a moment, it makes sense. You know, you can have an empty array and if it was nothing in the array, it can't have a first element. Since this is an optional, what would be good practice here? That's right. You want to safely unwrap this value. And this is a good opportunity to use optional binding like this. Hey, there, you're cooking the debug console. Great. So another handy thing you can do is check to see if an array contains a specific value. For example, do we have a donut? We do. What about lasagna? We do not. And that's good unless it was like chocolate lasagna. And that would be okay. Now I showed you two ways to append value to an array, but that just adds elements to the end. What if you want to insert a value somewhere in the middle of the array at a specific index. There's a method for that too. It's called insert or insert at. You simply call insert on your array and then passing the value you want to add followed by the index you want to put it at using the word at. Now tart is the first element in your array pastries. And if you don't want something in your array any longer, you can also remove specific elements. You can remove something at a specific index with remove at. And you can also remove the first or the last element. These both have special methods. So when you use these methods, you can see they return the removed elements in case you wanted to look at what you've removed, So it can see in the results and the right that remove two still contains our Danish and remove last has the eclair. So you can add and remove elements, but how do you change them? So you can use sub scripting to change the element at a specific index, or you can use a range to change multiple elements at once. So that's a neat trick. You placed two elements at index zero and one with three elements, a brownie fritter, and a tart. So you notice that it didn't sneak and replace anything outside of the range of indexes that you specified. They're still there. So you don't have to worry about shuffling things along in the array yourself. Swift takes care of that for you. So let's try swapping the positions of some pastries. There's a long tedious way. You could probably figure it out yourself. You could remove an element, store the result and then insert it again. But there's a handy swap at method on arrays that you can use instead like this. So that takes two indexes and just swaps values. So tart and fritter are switched around. And that was a lot to take in. I recommend keeping this playground around as a reference, but you should also look into the documentation of arrays yourself to see what other properties and methods are available to you. Now it's time for an array of many challenges about arrays.