7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

4.3 Array

One of two primary collection types found within the Swift standard library is the Array. Arrays can be very useful in holding on to several instances of values of the same type. And the nice thing about the Swift language being type safe is the fact that the Array type is generic. So you will always know what type of values are in your Array.

4.3 Array

Let's being working with arrays. Now, an array is nothing more than a collection of a bunch of values of the same type, just grouped together. So let's start to see what that looks like. So I can create a new instance or use var and I'll say first array. Is going to be equal to, and I'll use the array keyword similarly to how I used string in the previous lesson, but an array is actually a generically typed type. So the way that we specify what type of values are in this array. We use the syntax where we, where we used the open close carets here and within there we specified the type. So in this case we are going to create an array of string values. So that as you can see here I created my first array and it has zero elements in it. So we can also use a couple different shorthand notations to do that, if you don't want to specify or write this syntax all the time. I can say firstArray is going to be equal to, and I can do an open square bracket and I can specify the type. So I can do something like this, and this will let the compiler as well as Swift know that firstArray is an array of. Strings. And as you can see here, there's no elements. Finally, if you want to initialize an array and give it some values right away, the way that you can do that is using another notation. I can say firstarray. And typically, if you're creating an array. That has predefined values in it, you're probably going to see it this way. You'll see and open square bracket and then we'll just start to put in some values. We'll say one, two, three and four. So there you go. Now firstArray is an array of four elements, one, two, three, four, and they're all strings. So this is what you're gonna, what it's gonna look like when you see. An array within Swift, you're gonna see a group or a collection of these values with the square brackets around them. So now that we've created an array, how do we start to get some data out? So let's say I wanted to get the first value out, or I wanted to extract maybe the first value or the last value, or something like that. How do we do that? That. Well, in the world of Swift, that's referred to as a subscript. We wanna get out the data. A subarray or a subscript of values from that array. So I can say var first value is gonna be equal to, and I'll say firstArray, and then the notation for this is going to be. open square bracket, and then the index of the value we want to retrieve out of this array. Now, like many other programming languages, you definitely have to be careful because Swift is going to be zero indexed, which means the index of the first value is zero and then one, two and three. So I wanted, if I wanted the first value. I would want what's at index zero. So now you can see that the first value has the value of one, and then var, if I wanted to get the last value, I would need to say firstArray. Now, remember, I can't say for. Because this is going to give me an error, because this is at zero, one, two, three. So I, I would actually want to get what's at index three. So now you can see last value is equal to four. So that's nice if you want to retrieve a single value, but what if I wanted to get a couple of different values? Well, I can do that by using the range of indexes from within that array. And, if you saw, in a previous lesson, we started to introduce the concept of a range by using these three periods. That's a very simple way for us to represent a range within Swift. So, I can say var, maybe say firstThree. And we'll set that to be equal to firstArray. Now remember, that's going to be starting at index zero and we want the first three, which means we want zero, one, and two. So I'll use the three periods and say two. So there you have it. Now we've grabbed the first three values out of our firstArray by specifying. A range, and that's going to be one, two, and three. Okay that's nice so we can retrieve values out, but what about adding values in? Well, there's a very simple way to do that as well. There's a little bit of a helper method on these arrays known as append. And what append is going to do is it's going to add a value on to the end of your array. So I can say, first array dot append, and I wanna add a new element and this time, I'll say five. So now, first array contains one, two, three, four and five. So as you can see, all the values are right here. But what if I wanted to put in a particular value at a location so not necessarily at the end but maybe I wanted to put it at the beginning. So now I can say firstArray, and this time I want to insert, and I'm going to insert a new element at a particular index. So my new element is going to be zero. And we're going to put it in at index zero. So now we have zero, one, two, three, four, five, so our array is successfully growing by being able to add things on to the end or at whatever index I would like to put them. Well that's really nice, but what if I need to take things out? What if I need to remove values that I don't need anymore? Well we can do that as well. So the opposite of the insert is going to be something called remove at index. So I can say I want remove something from a particular index. So let's say I don't need that zero value anymore. So I'm going to remove the value that's at zero. Now if i were to just type out firstArray again, you're going to notice that i, it is gone but what was actually returned was the value that was removed. So I can actually specify this and say, I can say var removed value is equal to that, and I can ask for what as that removed value at the end of that operation. And there you go. It was zero. So now we're back down to our firstArray. We have one, two, three, four, five. Well, what if I wanted to undo this append? Well, I can use another helper method called RemoveLast, so that'll just basically pop off the end of that array, whatever was there at the end, and as you can see here, it was five. So if I ask again what's currently in firstArray we're back down to what we started with, one, two, three, four. So those are some basic operations to do by getting data in and getting it out. But what if we wanna do something maybe a little bit more interesting? So let's say. One of the problems we have when typically working with arrays is, yeah I wanna be able to get values out of here. But how do I know the length of this array so I don't goof up and say, I wanna get the fourth value out of here. Cuz remember if I try to get the fourth value this is going to be an error. So how do I know that that's not a, going to be a problem. [SOUND] Well, I can say. Var length is going to be equal to firstArray. And I can get the count of the number of values that are within that particular array. And now I can say var last. Since I know there are four values and it's zero indexed, I can say alright, well give me firstArray and I want whatever is at length minus one. So there you go. Now last to the value at the end of the array, is four. So now I can get in there by using the count property to figure out the length of my array. And then of course, you can do the other operation similarly to how you did with the string. I can say, all right. I have my firstArray. Is it empty? Well in this case, obviously we know it's not because there are four values in it. Another interesting built-in feature of the array type within the Swift standard library comes in the form of a couple built-in methods or functions that allow you to do some fairly interesting and beneficial operations on your array. So let's look at a couple of those. So if I were to dig in to my firstArray variable I'm gonna see that I have something called sort. Now there's two different versions of this. There's sort and sorted. And really the only difference between the two is the fact that sort is an inline sort which means it's going to sort all of the values. Within your array, without returning a value, they're just going to do them all in line and then sorted is actually not going to modify the original array. It's going to return a copy of itself, with all of the values within the array sorted in a particular order. So let's take a look at one as they're both fairly similar and we'll just kind of breeze through this a little bit. Now as you can see the parameter on sort here is, is ordered before and if you remember in previous lessons on functions and closures you're going to recall this syntax here. This open and close parenthesis with two values in between, as well as this arrow and, type is a function type, which also represents a closure. So within here, you can insert as a parameter either a function, either a variable that has a type of a function type that represents some sort of function. Or an inline closure and since I think the better way to do that, at least as far as learning and getting practice is concerned, that's going to be using a closure so that's what we're gonna do in this case. If you don't remember what a closure is you can head back and rewatch the videos on functions and closures to kind of refresh your memory a bit. But it's actually quite simple. The syntax is once again an open and close curly brackets and then the parameters are gonna go in between open and close parenthesis. So within here, remember as far as that sort method is concerned if I just bring it up again here. FirstArray dot sort is going to take two inputs and return a boolean. So those two inputs in our case since we're going through this firstArray are actually going to be strings, so what we can do here is we're going to say and actually a better way to think of it is that. They are going to be of type t. And if you recall when we created this firstArray way back in the beginning, we specified the t in this case, or the generic argument or parameter to be a string. So, we know that all of the values that are going to go into this closure. As far as inputs are concerned, or parameters, they're going to be of type String. So what I can say here is we'll say v1 for value 1 is gonna be of type String. And then we'll have a v2 that's also going to be of type String. And the recall that we specify the in key word. To specify we're going to start the body of our closure. And now, within here, I can say whatever sort of logic I want to provide to this sort method to allow it to determine how to sort, so we can put in a lot of sort, lot of complicated logic here, but once again. I'm gonna keep this relatively simple and I'm gonna sort this in ascending order which means I want this to start from the a's and go to the z's. I'm going to simply return that the first value is going to be less than. Then the second value so v1 is going to be less than v2. So I'm going to go ahead and comment this line out so that everything runs okay. And now, as you can see here, after this sort has executed, we now have four one three two, so as you can see, they have been sorted in ascending alphabetical order. And to prove that this was done in line, I can come down below. And I can go ahead and say first array so I can inquire as to what the value of that is and as you can see here it actually changed it in line. So now if I wanted to use the sorted method I'm going to say var reverse sorted array. And I'm gonna set that to firstArray, and this time I'm going to use the sorted method, and we're gonna do, basically do the same thing, so I'm actually going to copy this code that we used here for the sort method, as it's going to be relatively similar. But in this case I wanna return that. V1 is actually going to be greater than V2. So as you see here we're going to reverse order two, three, one and four, but if I were to look at what is in the firstArray variable you're gonna see it's still four, one, three and two, and that reverse. Sorted array now contains the reversed order array. So as you can see that sorted method, or function, returns back that value. Where as sort does not. So this is kind of nice to be able to do a reverse or something like that. But you don't have to do those manually. Because luckily for us that firstArray, or that the array. Type already provides a reverse method for us, that as you can see, is going to return an array of type T, and it's going to return a reversed ordered array based on what you have within, based on the contents within your array. So that's quite a bit of functionality built into a type that is provided for you out of the box, which is very, very useful. As arrays are very powerful structures that you're going to wind up using quite a bit within your applications, and they can be very beneficial. So definitely don't feel like you have to reinvent the wheel, use the functionality that's out there. But just kinda keep these concepts in mind as it's gonna become very, very important for you to be able to do basic operations to. Create arrays, to add things to arrays, get things out of them, and then ultimately sort them and play around with their order and things of that nature.

Back to the top