Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:30Length:4.8 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.1 Intro to Collections

There will definitely come a time when you will want to be able to group together values and work with them as a single entity. Swift handles this with collections. There are several built-in collection types in Swift, with the most foundational being arrays. Let's take a look at some of the basics of arrays now.

3.1 Intro to Collections

Now that we've had some time to go through some of the basic built in types when it comes to the Swift programming language. It's time to up our game a little bit and start to talk a little bit more about some more powerful types that are built in to Swift and that has to do with collections. Now, collections are really just a way to combine a number of other types into a group that we can treat as a single unit. Now, there are traditionally three different types that are available to us within Swift. But as you start to look around the Internet, you'll start to find that there are more and more available in different libraries that are available to you, as well as custom ones that you can build yourselves. But for the most part we're just gonna be dealing with some of the built in ones at the first shot. So the first one that I wanna talk about is an array. Now an array is really nothing more than a very simple collection of a specific type, or of like-typed objects or values. So what do I mean by that? Well, an array is really just a collection of all ints, or all strings, or all unsigned ints, or all doubles, or floats, or bools, or strings, or what have you, all contained within a single unit that we can deal with all in one shot. So how do we create these? How do we start to work with them? So the first thing that you're gonna wanna do is you're gonna wanna create a new array, and there's a couple different ways to do that. So we're gonna talk about all of them briefly and then I'm going to tell you which one I prefer, but ultimately you can choose whichever one you would like. So the first example I'm gonna give is I'm gonna create an array called names. And I'm gonna set this equal to. And the first way that I can create this, is I can use the Array keyword. And then I can use the less than, greater than signs, and then open and close parenthesis. Now within the less than and greater than signs, I'm gonna specify the type of this array or the type of the data that's gonna be held within this array. So for this particular example, I'm just gonna create an array of strings. Now don't forget the open close parenthesis here at the end. Because if you don't, you're gonna get a little bit of an error here saying hey, you forgot to use the open close parenthesis if you're creating a type here, so go ahead and add those in. So this will work, and I can kind of see here that I'm doing some type inference here, or Swift is doing some type inference here by. I'm not specifying the fact that names is going to be an array of string, but I'm going to assign it to a new array of strings, so that'll work. It's a little bit verbose, and it's kind of ugly if you ask me, it doesn't really fit in with the rest of the language necessarily. But we can use a bit of a shorthand notation to kind of get around that, make it a little bit more Swifty, if you will. So I can get rid of all of this. And now traditionally when you're talking about arrays, whether you're talking about within Swift or other programming languages, you're typically going to associate them with the square brackets. So open and close square brackets with the type of the array in there, so once again, the string. By doing this, I'm also able to creat ea new array, so I can go ahead and build that and everything will build fine. Now this will create another array, and we're going to use implicit typing again, or inferred typing again. And names is gonna be an array of strings, and it's going to be initialized, in this case to, well, an empty array. Now there's a final way or a third way that we could do this as well. And a couple variations on it, really. But if I wanted to be a little bit more explicit about it, I could actually use the Explicit typing version. And I can say the name's is going to be an array of strings. And then I can set this equal, I can leave it empty like this if I like. Or I can also initialize it to the empty array. So any of those three are absolutely going to work. Choose whichever one you like the most, because ultimately you're going to spend the most time with the code that you're writing. But this kinda happens to be the way that I like to do it, and actually because we're using type inference, and we're using the open-closed square brackets, I tend to lean towards this way. But it's completely up to you. Choose whichever one you would like. Now you're definitely looking at this, thinking to yourself right now, well, all these square brackets here. This could be a spot for type inference. I'm gonna get rid of this string but don't do it only because this is a little bit ambiguous because it's not able to determine the type here. Because there are other ways or other data types specifically another collection type called a set that you could basically create the same way so we don't want to do quite this way. Unfortunately that's not gonna work. So you're gonna have to use one of those variations that I showed you. So now that I have this array how do get data in? How do I get it out? How do I access it? Well it's actually quite simple really. There are a couple of different ways. So as you see here I have created this empty array, but I don't have to create it or initialize it as empty. I could start putting things in here, so I could say Derek. And this is gonna take in a comma separated list or array of data here, so I could just keep adding different values in here like this. And now my string array called names has three values in it. Derek, Robert, and Billy. So it's been initialized. To have three values. Now as you're going around and working with these things, you may need to check at some point if this is an empty array to be able to work with it, and it just so happens. There's an empty property that you'd be able to use if you would like to do so to be able to check to see whether or not it's actually empty. And then once I've gotten some data in, or maybe after I've initialized my array, maybe I want to add some data to it. So there's a couple different ways to do that. I can use the append function here, and I can append to this another name. I could say I want to append the name Sally into my names array. Now I could also do that another way. Instead of using the append function, I could actually use a short hand notation using the plus equals operator and I could append a single value, but I could also append an entire array onto my names array. So I could use the open close parenthesis here, and I could start adding more values here. I could say I wanted to add Joan, and Ellen, and anybody else that wanted to come to the party. I could continue to concatenate onto the names array, additional values found in another array. So let's say I was done with all this stuff and I want it to clear this out I could simply say name is equal to the open and close square brackets here and that would effectively empty out the array and give me a brand new array that I could start to work with. So now I'm back down to empty again and you can once again, if you wanted to, you could check the is empty property and then that would ultimately tell you that is empty is true. But then let's say I wanted to reinitialize this guy again and I could say names once again Is gonna be equal to, and let's just reinitialize it to what it was before. So let's go ahead and copy this we'll paste it down here. So now we have this information in here how do we get it out? How do we get this information out? Well depending on what your ultimate goal is do you truly wanna remove something or do you just wanna get access to a piece of data. Now if you just wanna get access to a piece of data, you can use our subscript notation which is, I'm going to specify the name of my array,and then right up next to it, i'm going to specify open and close square brackets. Now within here I can specify an index of whatever value I want to retrieve from this particular array. Now remember, just like so many other programing languages, Swift is no different in that it's zero based. So that if I wanted to retrieve the first value out of my array this would be index zero, Robert would be index one and billy would be index two. So to get the first value out I could say I want to get the value at index zero and I could get that name, and at this point, Derick would be stored within name. Now that's fine, but what if I actually wanted to remove Derick, I wanted to get rid of that value instead of just getting access to it. Well I could use another function. I could say names.remove and there's a couple different variations of remove, I could remove all which will effectively remove all the elements and clear it out. Or I could say I want to remove at an index and once again this is gonna be zero based so I would remove what's at zero. Now one thing to make note of here is this remove at index takes in an index now. At this point, at compile time, I could be putting in here things like five, and as you can probably guess at this point, this is a little bit too far, there is no index five. So, if we wanted to get around that, we could do something like name equal to name.remove.index and ultimately what's gonna happen here is we're going to be retrieving back an option that's going to, that's either going to contain nil because there wasn't a value at that index or we could be retrieving the actual value that was found at that index. So it's just one thing to kind of remember that you might have to do at some point while working with these arrays if you actually wanted to take something out. Now the opposite of being able to remove something would be being able to insert something. And I can also do that as well. I could do names.insert and I could insert a new element at a particular index. So if I wanted to add in John and I wanted to add him in at index one, then we would have a new value John that would be inserted into index one and everything would kind of effectively move down a little bit. So it's just some of the basic operations and the concepts behind an array. And one of the important things to remember about arrays is that arrays are fairly general, fairly basic, and you can stick any sort of data in there as long as it matches the data type. I could enter in duplicates and do all that sort of thing and ultimately everything is gonna continue to work.

Back to the top