This course will provide you with a comprehensive understanding of the fundamentals of Swift. We're going to learn about variables, constants, arrays, dictionaries, sets, if statements, and more!
This course is designed for anyone who wants to:
- Learn about iOS development and coding
- Move into a career as an iOS developer
- Master Swift skills
To get the most out of this course, you should have some basic knowledge of iOS.
Hi. Within this lecture, we're going to learn something new and it's called Arrays. So, in order to do that, I'm just going to save this playground. I'm going to close it down. Of course, you may want to continue from this one as well but I don't want to make it too long because it will get hard to actually keep track of what we're doing. So, I'm just going to create another playground. I' m going to create an Standalone Playground for our arrays. I'm saving all of these into my iOS Complete Folder. Later on, I'm going to share all of these playgrounds with you. We are gate up. So, let me go to my playground's folder and this was named Variables. Now, I'm going to name this as Array. So, what is an array and how we can use those arrays? So, let me create this and I will tell you all about it. Don't worry. So, arrays are actually collections of values, collections of data types.
So, let me run this and see if we have something to run here. No. Everything seems to be working. So, I'm going to start by deleting this line and rather than creating a simple string over here, I'm just going to create an array. So, what are collections of data types? So, let's suppose that I have to store my favorite movies in something like this. So, if you say myFavoriteMovies. You can just open a brace, open a parenthesis but not like this, not a curly brace, but rather just like this. So, you do it with Option and eight or Option and 7 depending on your keyboard. And if you close this, that's how you create an array. So, in here I can write more than one value actually. So, for example I can just say "Pulp Fiction" and this is a string and I can just do a comma and I can continue as long as I want. For example, I can go for "Kill Bill". And again with a comma, I can just go for another movie that I'm fan of like "Reservoir Dogs".
So, here you go. I have created a favorite movie array. So, I could have done that with different variables. So, I could have gone like myFavoriteMovie1, var myFavoriteMovie2, myFavoriteMovie3, and assign those values to different variables. But what if I have like 1000 value? Then it wouldn't be efficient to create 1000 variables at one time. It wouldn't be efficient. It wouldn't be easy and this is actually easy. Because let's suppose that I am downloading these values from Internet rather than just writing those values. So, if I can download a list of a favorite movie of someone or of mine, I can just store all of these values actually, into one variable rather than creating everything one by one. And I cannot even do that. I can go for var myMovie1 is this, var myMovie2 is this but I don't know the exact amount of the movies that is going to be downloaded from the Internet in most of the cases. So, in an array, I can store as many as I want.
So, I can use this arrays and I can work with collections of values efficiently. So, that's why we are using arrays in programming languages. You can see arrays. You can see lists. You can see these kind of values data structures in almost all programming languages because they are so efficient. You just create one variable and you store as many as data you want in them. So, I'm just going to delete this variable over here because I'm not going to do anything with this. For example, if you say myFavoriteMovies, as you can see this is now an array of strings because all the values in my array are actually strings. So, we just want to keep it that way most of the time. But, if I want to can I add another value here like 5. Actually, I can do that. If you open this warning over here or error, it gives you a suggestion. It says that Insert 'as [Any]' because this is not a homogeneous collection. This is a heterogeneous collection right now.
So, what does 'as [Any]' mean? Let me fix this. Let me add this and I'm going to explain what it is and we're just going to use this technique in some other instances as well. So, Any means any type. We don't know the type yet. So, this is either a string or a boolean or an integer and as means, cast this value as 80. So, this was originally array of strings. So, this is now an array of Any. So, as means casting and Any means any object, actually. So, this is an array of any object. We don't know the type. We don't care what the type is at this moment and we can save anything we want in this array as long as it makes sense, obviously. For example, I can go over here and add a boolean for God's sake. And so, in myFavoriteMovies, I have a string, I have an Integer and even I have a boolean. Any means literally an any object. And we're going to use this casting and we're going to use this Any later on.
So, now if I do myFavoriteMovies, it will show my array as an Any object. So, if I do, myFvoriteMovies, it means that get the first element and show it to me. And it starts with 0, yes. And this is called an Index and the Index of an array starts with the number 0. It goes 0, 1, 2, 3, 4, 5. So, I can just say myFavoriteMovies. Let me play this. As you can see on the right hand side, we get the values. Respect the values. So, this is called Indexing. myFavoriteMovies gave me "Pulp Fiction, Kill Bill 1, Reservoir Dogs Tree. And if I go for the other elements, I can get them as well. So, let me try and get them as well actually. So, myFavoriteMovies. This will give me Reservoir Dogs. myFavouriteMovies3, this will give me 5 and myFavoriteMovies, this will give me 2. So, let me run all of this. So, 0, 1, 2, 4. Over here, 3, 4. And can I do something like myFavoriteMovies. No, I cannot do that.
I already have what I have in my array over here. It will give me an error. As you can see when I hit over here, I cannot reach the methods or attributes or options of string. So, I cannot do something with 'Pulp Fiction over here. Why is that? Because it thinks that this is not a string. This is an Any object. I can try to manually write this on my own but as you can see, it says that value type of Any has no member uppercased because this is not a string for Swift. This is an Any object. And this is basically why we try to keep the same type values inside of an array in order to reach their methods to reach their attributes and change them and do something with them. I can have heterogeneous values as here with casting as Any but if I have the same values inside of an array, like a string array. So, let me say that var myStringArray rather than myFavoriteMovies. So, I'm going to go for ["Test", And "Test2" and one more "Test3"].
So, this is actually an array of strings. If you do myStringArray, you will see that this is actually an array of strings. Now, I can go for myStringArray0 and this will give me Test. Now, I can reach those methods. I can reach those attributes and functions and everything. So, if I say uppercased and if I run this, now, I will see that I have converted this task to be uppercased. So, you can use this very efficiently with arrays. You can just call what element you want and you can do whatever you want to do as long as they're the same objects. They have the same values. So, both technique has their own advantages and disadvantages. You may choose when to use, which to use. So, let me show you something else. You can do something like this. myStringArray.count and it will give you the current count. How many elements are there in that list, in that array.
So, you can do something like this: myStringArray and . And rather than saying 2 over here, if I say 2, I will get "Test3". I can just try to get this with myStringArray.count. And in fact, you may try to pause the video and get it on your own. Rather than saying 2, I can just go for myStringArray. [myStringArray.count -1]. So, myStringAyray will give me 3 and if I subtract 1 from that, I can get the same result over here. So, this is useful because I can maybe add some more over in my string area. I can just go for "Test4" and if I run this, this will give me an error because I forgot to add the quotation mark at the end. But if I run this right now, I will still get "Tasks4". I will still get the last element. And as you can see, I got "Task4". So, you can use this. An, of course, there's another method for that as well. I can just go over here and I can say myStringArray.last. So, this will give me the last object, last element. So, I can get "Tests4" over here. Maybe I want to get [myStringArray.count -2]. Then it makes sense to use this technique. I can get "Test3". I can get, not the last but the one before that using this [myStringArray.count -2]. So, what else can I do with string arrays or arrays in general? I can do a sort and this will sort the array. And let's see. I get "Test", "Test2", "Test3", "Test4", and since this is a string test, it will sort those tests alphabetically. So, let me change those values and we'll see. We'll test that. So, "Test6", "Test2", "Test1", "Test4". So, this is not in alphabetical order right now.
So, if I run this, it will recompile the code. And as you can see, my values has been changed. But if I run the last line over here, I still get the same result. Test 1, 2, 3, 4 or test 1, 2, 4, 6. So, this is a alphabetical order right now. And if it was an integer array, it will just order. It will just sort the array based on the values of the integers. And we have been working with strings to this point but of course, we can do something like an integer array. So, let's call this myNumberArray and I can go for 1, 2, 3, 4, 5, 6, 7 over here. So, this will be an array of integers. It's the same principle. You can just add whatever you want to add inside of an array. Now, we can stop here. That's it for arrays at this moment because we're going to use arrays in most of the apps that we're going to build. Anyway, we're going to get to practice them so much. Now, we will continue some other array, like data structures, like sets and dictionaries and we're going to learn what they are in the next lecture.
Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.