‫Instructor: Hi, within this lecture
‫we are going to learn something new
‫and its called arrays,
‫okay?
‫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 going to create another playground.
‫I'm going to create
‫an standalone playground
‫for our arrays,
‫okay?
‫I'm saving all of this into my iOS complete folder.
‫Later on I'm going to share all of these
‫playgrounds with you via GitHub.
‫So, let me go to my playgrounds folder,
‫and this was named variables,
‫now I'm going to name this as array,
‫okay?
‫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,
‫collection of datatypes, okay?
‫So, let me run this and see if we have something wrong here.
‫Nope, everything's seems to be to 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 datatypes?
‫So, lets suppose that I have to store my favorite movies
‫in something
‫like this.
‫So, if you say "where my favorite movies?"
‫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 seven, depending on your keyboard.
‫And, if you close this,
‫that's how you create an array, okay?
‫So, in here I can write
‫more then one value actually.
‫So, for example I can just say Pulp Fiction,
‫with quotation marks, 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, okay?
‫And again, with a comma I can just go for another movie
‫that I am fan of, like Reservoir Dogs, okay?
‫So, here you go.
‫I have created a favorite movie array.
‫So, I could have done that with different variables,
‫right?
‫So, I could have gone like my favorite movie one,
‫or my favorite movie two, my favorite movie three,
‫and assign those values to different variables.
‫But, what if I have like a thousand values?
‫Then, it wouldn't be efficient
‫to create one thousand variables
‫at a one time,
‫right?
‫It wouldn't be efficient, it wouldn't be easy,
‫and this is actually easy.
‫Because, lets suppose
‫that I'm downloading these values from internet,
‫rather then just writing those values.
‫So, if I can download a list,
‫okay?
‫Of a favorite movie of someone, or of mine,
‫I can just store this, all of these values actually,
‫into one variable,
‫rather then creating everything one by one.
‫And I cannot even do that.
‫I can go for mine, var my movie one is this,
‫var my movie two 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, right?
‫So, in an array, I can store as many as I want.
‫So, I can use this array,
‫I can work with collections of values efficiently,
‫so that's why we are using arrays in programming languages.
‫And, 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 as 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, "my favorite movies"
‫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, five?
‫Yeah, 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 homogenous 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 are 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 any.
‫So this was originally a string of array,
‫array of strings, right?
‫So, this is now an array of any,
‫so as means casting,
‫okay?
‫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 sake, and so in my favorite movies
‫I have a string, I have an integer,
‫and even I have a Boolean.
‫So, any means literally: in any object, okay?
‫And, we're going to use this cast,
‫and we're going to use this any later on.
‫So, now, if I do my favorite movies
‫it will show my array
‫as an any object, okay?
‫So, if I do my favorite movies zero,
‫it means that get the first element
‫and show it to me.
‫And, it starts with zero, yes.
‫And this is called an index, and the index of an array
‫starts with the number zero.
‫It goes zero, one, two, three, four, five.
‫So, I can just say my favorite movies one, okay?
‫Let me play this, as you can see
‫on the right hand side, we get the values,
‫the respective values, okay?
‫So, this is called indexing.
‫My favorite movie zero gave me Pulp Fiction,
‫Kill Bill One, Reservoir Dogs Three,
‫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, my favorite movies two,
‫okay?
‫This will give me reservoir Dogs.
‫My favorite movies three, this will give me Five,
‫and my favorite movies four, this will give me true.
‫So, let me run all of this,
‫so, zero, one, two, four,
‫over here,
‫okay? Three, four.
‫And, can I do something like my favorite movies five?
‫No, I cannot do that.
‫I already have what I have in my array over here, right?
‫It will give me an error.
‫As you can see, when I hit that, over here
‫I cannot reach the matters, 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 isn't, 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 matters, to reach their attributes,
‫and change them, and do something with them.
‫I can have that our intergeneous 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, my string array, rather then my favorite movies.
‫So, I'm going to go for test, okay?
‫And, test two,
‫and one more,
‫test three, okay?
‫So, this is actually an array of strings, right?
‫If you do my string array
‫you will see that this is actually an array of strings.
‫Now, I can go for my string array zero,
‫and this will give me test.
‫Now, I can reach those matches,
‫I can reach those attributes and functions and everything.
‫So, if I say, uppercase, then 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 what ever you want to do
‫as long as they're the same objects
‫there's 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, right?
‫My string array dot 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,
‫my string array in open parentheses,
‫and rather then saying two over here,
‫if I say two, I will get test three, right?
‫I can just try to get this
‫with my string array dot count,
‫right?
‫And, in fact, you may try to pause the video
‫and get it on your own.
‫Rather than saying two,
‫I can just go for my string array,
‫okay?
‫My string array dot count
‫minus one.
‫So, my string array will give me three
‫and if I subtract one from that,
‫I can get the same result over here.
‫So, this is useful, right?
‫Because, I can maybe add some more over in my string array,
‫I can just go for test four,
‫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 test four,
‫I will still get the last element, okay?
‫And as you can see, I got test four.
‫So, you can use this and of course,
‫there's another method for that as well.
‫I can just go over here and I can say
‫my string array dot last.
‫So, this will give me the last object, the last element.
‫So, I can get test four over here.
‫Maybe I want to get
‫my string array dot count minus two.
‫Then it makes sense to use this technique.
‫I can get test three, I can get not the last,
‫but the one before that using this.
‫My string array dot count minus two.
‫So, what else can I do with string arrays
‫or arrays in general?
‫I can do a sort, and this will sort the array,
‫okay?
‫And, let's see I get test, test two, test three, test four.
‫And, since this is a string test,
‫it will sort those texts alphabetically.
‫So, let me change those values and we will see,
‫we will test that.
‫So test six, test two, test one, test four.
‫So, this is not in alphabetical order right now,
‫so if I run over this, if I run this,
‫if will recompile the codes,
‫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 one, two, three, four.
‫Or, test one, two, four, six.
‫So, this is alphabetical order right now.
‫And, if it was an integer array,
‫it would just order it,
‫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 my number array,
‫and I can go for one, two, three,
‫four, five, six, seven over here.
‫So, this will be an array of integers.
‫It's the same principle,
‫you can just add whatever you wanna add inside of an array.
‫Now, we can stop here.
‫That's it for arrays, at this moment because,
‫we are going to use arrays
‫in most of the apps that we are going to build anyway,
‫we are going to get to practice them so much.
‫Now, we will continue some other array-like data structures,
‫like sets and dictionaries.
‫And, we are going to learn
‫what they are in the next lecture.