Hey everybody. Welcome back to the coding party. Mark Price here at devslopes.com. Today, we're going to talk about dictionaries. What I'd like you to do before we start is actually do me a favor and do a Google Search. Type in, "How do hash tables work in computer science". "How do hash tables work in computer science" and go read some articles and start getting an idea of what a hash table is. Then pause this video and go do that and come back here like in 15 minutes because I want you to have a base foundation. Did you do that? I'm sure you didn't because non of you listen to me until you start getting those job interviews and then they're like, how does this work? You're like, "Mark you were right this whole time. I should have been listening to what you said, and read about this and this and that and that, but instead I just was hoping that you had a video on it and whoa. But anyway I went and read it and I got the job." That's what always happens. Don't listen to me, listen to me. Whatever, do whatever you want, it's your life. All right. So I'm creating a new playground here and we're going to call this one dictionaries. So in Swift a hash table is called the dictionary and in different languages they're hash tables or hash maps, or in this case dictionary here. I'm going to go ahead and store this in my dictionary's folder. There's our playground and I'm going to pull up in this nice little info-graphic that Apple has created. So there are three major collection types in Swift. We have the array, which is an ordered set, excuse me, an ordered list of items and you can have multiple values of the same type in an array. So for instance, Index 0 can have let's say the word six eggs, but also Index 5 could have the exact same word six eggs, doesn't matter. A linear list ordered items. Now we've got something called the set. Now a set has items that should only have one type of value meaning the values need to be unique, but they're not ordered, but they are unique. Then we've got this cool thing here called the dictionary. Now you're going to use dictionaries and arrays more than your sets when you're working with Swift. But the dictionary has keys and values. We're going to have an example similar to this today, basically where the key is of a certain type and then there's a value that's related to that key. So the values can be the same as the other values of the other keys but the keys must always unique, and this is not ordered. There are actually very complex and efficient algorithm working under the hood that help you retrieve and fetch items. Imagine if you had a dictionary that had a million keys in it. It's not going to go through that entire list of keys. It's going to start breaking things down and segregating out sections until it finds the key in the fastest route, very complex things going on underneath the hood. Again, if you do that search I told you to on how do hash tables work there's some amazing YouTube videos on how this actually works underneath the hood: if you're into that kind of thing. Anyway, so we're going to be working with the dictionary today, and we're going to work with keys and values. So this is straight out of the Swift 3 iBook. Very important that you read that book, of course I've said that before and here we go. We're going create our first dictionary "namesOf-". That's not what I want you to do, "namesOfIntegers". Lets say we want a dictionary that lets us give a visual word that you can print out for a number type. Meaning like, let's say I want the number three to say the word three, things like that. So what I can do is I can say namesOfIntegers subscript, here the square brace here, and we're going to say three, equals three. Now I know what you're thinking, oh, this is just like an array. This is like the third index, right? No it is not. This is actually the name of the key. I just made the names of the keys of type int. This has nothing to do actually with an array. It's not a subscript in that you're grabbing an ordered amount of items. This just happened to be the type I chose for the key. This could also have been a string here which is interesting. So this is a simple example of your very first dictionary, and what we're doing here is we're just creating one on the fly, initializing it using type inference. We're creating a dictionary with a key of type int and a value of type string. I would not be able to do something like this and put a three in here because the value needs to be a string. Cannot assign value of type int to string. So keys and values, all this may look familiar to you if you've worked with arrays. Names of integers, lets do one more. Let's say 44, equals 44. So the idea behind this dictionary is there's a unique key and then there's a word value for that. We're just explaining in words. So simple example. If you want to quickly clear out a dictionary, empty all the items out of it. It's as easy as doing this. Square braces, colon, square braces, it's going to clean out all of your keys and all of your values. Nice and new. Let's do a more real-world example. Let's say we have an app. Let's talk about airports of the world. We want to have an app that has all the airports of the world. Well, every airport can only have one unique airport code. Imagine if airports had the same airport codes it would be crazy, like imagine if Ireland had an LAX, Los Angeles had an LAX, and Dubai had an LAX, it wouldn't work. It would mess things up. So luckily we have unique airport codes that we can use throughout the world and each one has its own name. Its own unique name and, you know what, maybe the airport codes are different, and maybe two airports can have the exact same name but that wouldn't matter because it has its unique code and that's the most important thing but we still need the name. So that's the advantage of using dictionaries, is the keys have to be unique: they have to be. So let's do that now. Let's create our dictionary. So var airports, and this is going to be of type string so we're going to do it in explicitly declared dictionary. So the key is of type string and the value is of type string and let's go ahead and initialize it here right off the back. So we're going to create some dictionary literals here that we're initializing right when we declare as well. So Toronto. So we've just created a dictionary that comes pre-loaded with one element in it or one key-value combination. But we want to do more. So we're going to put a comma here, and then LAX, and then what we're going to do is say Los Angeles. Pretty cool. Actually Los Angeles and say international. So I actually comma does not go there. There we go. So what is happening here? So we put in the key name first, and then a colon. The colon separates the key from the value. So the key name goes first, the colon, and then the value. The comma separates the elements or the combinations here. So this is one key value combination, this is a second, and it's separated by comma. So key name, colon, value, comma, key name, colon, value, comma, key name, colon, value, comma. We didn't have to explicitly declare it like so, we could have used type inference, and it should just work out of the box for us indicating that yes, this is a string key, and this is a string value. Pretty cool. So now we've got our dictionary full of airports, and there's other things we can do with this. We can print the airports dictionary has, let's use some string interpolation, airports.count items. Let's make this a little bigger here so you can see. There we go. I put the quotation too soon. So the airport's dictionary has two items. Is that true? Well, yes it is, one and two. So you can get the count of a dictionary. What else can we do? If airports.isEmpty, you can see if it's empty or not. We can print the airports dictionary is empty. So you can check if it's empty, the simple little command, and you're probably wondering, well, how do I add new items to a dictionary? Well, since we're not making an ordered array where an element goes onto the back of the list we are inserting at a certain place, we don't care about that, all we care about is a unique key. So we can just say airports, let's add a new one, LHR equals London. So what we're doing here is we're setting LHR, this is the key name. We're setting that unique keys value to be of the value to be London. So the unique key LHR is going to be equal to London. We just added a new element at this point in time. Well, what if we want to update this or override it? It's just as simple as doing the exact same thing. So what we did here was whatever was in that spot we've now overwritten, it's gone. So that's how you replace elements, that's also how you update elements. Did it matter if one of these key names existed before or not? No, it does not matter because it will be created the moment you assign it there. So if you're thinking well, if there's nothing in there and I tried to assign something to it it's going to give me an error, that's not the case. Unlike an array where if you try and grab an item that doesn't exist, you're going to get the crash, it's different here. What else? Let's say airports, slow this is up here. Say airports, create a new one, DEV equals Develops international. Didn't you guys know that Devslopes has its own airport now, and we have Wi-Fi and roller coasters, and birthday cakes? It's really cool. Then we realized it was a dream and it doesn't exist. So how do we actually remove an item completely? We want to go on, key in everything because there is no Devslopes airport with cakes, and Wi-Fi, and roller coasters. Although it'd be cool. We probably had a draft in there too, but my dreams are now crushed. So airports DEV equals nil. You can set it to nil which will completely destroy and remove the item. It is gone. So I hope you're starting to see, I can work with keys and values, they are unique, I can do different things with them. You're going to use these a lot, we're just scratching the surface here. You can also iterate through a dictionary which is cool as well. You can get all the keys and values out of it which we'll do right now. So like I will say that here, for each airportCode and airportName in airports. So what we're doing here is a for-in loop. The for-in loop in this case in a dictionary it's giving us what's called a tuple. A tuple is a datatype or a data construct that has one or more elements in it. So it's returning us a tuple you don't have to remember that, just know how to use this. What it's going to do is it's going to pass in the key name, and the value. I could also have said key and val, same exact thing. So it's going to give us the key name, and the value, and it's going to go through the entire dictionary on each one of those and there is an algorithm that goes underneath the hood that's efficient in grabbing this information, though not as efficient as other ways of accessing information. In many cases, you don't want to iterate through an entire list of dictionaries. If you want to iterate through a list of something, maybe an array is better data structure, but you can definitely go through here and I have before. So I'll say airportCode, and then we'll put up the last colon here, and then we're going to say airportName. So we're just going to go through and print the code and the name, or the key and the value. Here it is. There's the key, there is the value. There's the key, there is the value. Key and value. Looking good? Very good. So there's our airportCode, airportName, perfect. What else can we do? We can also just do the keys and values individually. So for each airportCode, or we can even just say for each key in airports.keys, so you can print out the keys. We can pass in the key. Pretty cool. We can also do the same thing with the values, for each val in airports.values. Now, the val is just a variable that I've created a constant. Value, and we're going to pass in val here. So value, value, value. So you can work with keys and values. The keys are unique. Most of the time you're going to work with ints and strings here. In your datatypes, you can play around with other things. But one of the big uses of dictionaries as when you're working with JSON, data that you're getting from the Internet it's going to come down in unbundled JSON file and you're going to grab things by keys and values because JSON is very much similar to the notion of dictionary keys and values. If you don't know what I'm talking about, take a look at it right here. We can go to let's say Star Wars API, that's this free API that someone's created on the Internet that has lots of information on it. So right here it's pulling up some JSON, this one is Luke Skywalker. So this is a dictionary where the keys are of type string. So here's a key code name of type string. So name is the string Luke Skywalker, so the value type of this one is a string. Let's find one that's not a string here. These are all strings on here. Let's look at some other API features to show you how it's going to translate in your code. These are all strings as well, they could also be numbers. This company or this person chose to put all the values in as strings instead of numbers. So that's really important too to understand this. When you download information from the Internet or JSON, you have to abide by whatever they have created for you. So whereas I might have put this as a number an integer, they chose to use a string for both the key and the value. There's another API that I like to work with as well, Pokemon or actually Poke API. Another one that somebody put up which is cool. Here's one and perfect example. So this is a dictionary object, and the keys are all strings. But in this case, so the key ID has a value, that's a number, it's an integer, and base experience is an integer, height is an integer, is default is a boolean, so you're going to have a boolean value type. So this is interesting too. Look at this one. On this one, the key name is a string, but the value is an array. That's really interesting. Can we do that with Swift? Let's say we wanted to create abilities, that's really interesting. Let's give it a shot. I'm going to say var, I'm going to say abilities, it would be a dictionary, right? So the abilities dictionary is going to be a dictionary of type string, and then what we're going to do is we're going to say an array, and the array is going to be an array of what? It's going to be an array of dictionaries. So let's see what it does here. I'll type string and string, I'm just curious. The syntax is wrong here. But anyway, my point here is you can have different items inside of your dictionaries, such as a boolean and int. You can do all different things with your dictionary. So it doesn't have to just be strings and integers. So lots of cool stuff. This is just scratching the surface here, just touching the surface. If you want to get a better synopsis of how dictionaries work, again, do that Google search I told you about, how do hash tables work in computer science? You can also look at JSON and start getting a feel. When I say JSON, that's J-S-O-N, get a feel of how things work with web and how it might translate to your Swift. That's it. Lots of good information. Soak it all in, and let's move on and forward. Mark Price at devslopes.com. See you next time.