[MUSIC] Welcome back. In this lecture, we're gonna talk about some basic data structures. Namely, arrays and dictionaries that you'll use pretty much everyday when writing Swift analysis development. We've left off here last time with loops. And you remember here we sort of used an array. And array is just sort of list of objects, or numbers, or whatever you want to have. And in Swift, it's very different from some languages as in arrays are strongly typed. As in if you have an array of integers, you can only have integers in that array. You can't have an array that has like one integer, one float, one object and et cetera. Well, you could but you would have to type it. That would be a array of type any object, or any, because integers are not even objects. And when you have an array of type any it's not very nice. So you don't know what's in it and that doesn't feel safe. A lot of Swift is about feeling safe. So, let's show you what I mean. So, we're going to define a ray of animals. Let's see what I'm feeling today. We'll have cows. Sorry, that will be strings. A cow, dog, and a rabbit or a bunny. All right. So what I did here was using the square brackets, I defined an array. And this is basically the syntax used to initialized it. You put whatever you want inside it and separate them using commas. So as you can see here, I have an array of animals. And if you look at the type of animals, it is a type of array, of string. So, that's very interesting how it looks. And that's how you actually would type it if you were to explicitly type it. It's just a string and by putting brackets around it, it becomes an array of strings. And this can be extended. You can have an array of an array of strings. And you can have an array of an array of an array of strings. You can do that as much as you want. But in this case now, we're only going to talk about an array of strings. All right, so what can you do with this? You can access them, for example. So what is, an array is an indexed list, right? So it's ordered. What is the first one? Of course, the first one means zero with one. And, as you can see, first one is the cow. Second one, index one you have dog, index two you have bunny. All right. So that's how you access them. You can also set them like this, right now our animals array as a constant. We can change that easily to a variable. And now we can set animals array two, we're gonna replace it with rabbit. I don't know, it seems like a more proper term. And once you, sorry, you don't replace it with an array rabbit. You replace it with a rabbit. And actually let's go back to that. And like I said it's strongly typed, so you can't actually set it to an array. Set this sort second array. It needs to be a string, which means you have to remove these brackets here. And now nothing really prints here and that's because animals was modified down here. So if you print animals again, now we have Cow, Dog, and Rabbit. And that's basically it. And combine this with what we had last time, which is the animation, fast animation. If you type for animal, and animals. Animal. You'll get animals printed and you can see here. And you only see one here, you have right-click on this. Go to Value History, and you'll see that in the first loop it printed Cow, second loop Dog, third Rabbit. All right, so enough of arrays. Let's look at something a bit more interesting called dictionaries. A dictionary can be like a real dictionary is sort of a lookup table. There's a lot of names for this. You can call it a hash. It's generally unordered, not like a real dictionary where you look up words. But the idea is similar. The idea is that you map a key to a value, where say in a real world dictionary. The key would be the word and the value would be its definition. And so how you define that here is very similar too. All right, so we're gonna define a dictionary that sort of takes the key, which is an animal, and it returns its cuteness. It stores like how cute an animal And so we have a dictionary called cuteness. And you define it with the square brackets again. The difference now is that you use a colon to specify the value of each key. And cows are, I guess, not very cute. And you use the comma to separate again. And I'm gonna go to a new line to make this nice and clean. Dog, I mean, people have opinions, but dogs are generally cute. And bunnies are generally very cute. All right, so we close that off and it looks kind of weird now. We can fix that up a bit by tabbing this in, tabbing this in. Now we've got a nice dictionary. And if you look at it from here, it's basically three. So they're not ordered, so it's just dictionary of three keys that map to their corresponding values. And you access them a lot like you do an array. For example, cuteness of a Dog and it is Cute. But something interesting here. So there's a slight difference here. In an array that has three objects, what if you try to access the fourth one? It will actually error on you. You cannot access out of bounds in an array. [INAUDIBLE] Access for here, we're gonna get an error and it's actually a crash like the compiler won't be able to catch this. It doesn't know the length of your array, but when you run it, it will crash. When you look at a dictionary, what if we tried to look at the cuteness of a cat? This actually won't crash. It'll actually return a nil, which means that whenever you look up a dictionary, you get an optional. And we can see that if we say let dogsCuteness. If we assign this to a variable, this variable will actually be a String?, an optional string. And that is exactly because, now you might not, like it's not guaranteed to have it, the key inside the dictionary. And so that's something to be aware of. Otherwise, arrays and dictionaries are very similar. And to get a bit fancy here, we're going to print out the cuteness of all the animals using this fancy for loop that we had before. For an animal in animals, we can index cuteness using our variable animal. What do we get? We get very cute and that's the last value. Not very cute and Very cute. So that's our dictionary. And this is an array. And I hope you enjoyed the lecture.