7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

4.4 Dictionary

In this lesson, we're going to discuss one of my favorite types within, really any sort of programming language that I've come in contact with and that's a dictionary. I think a dictionary is a very versatile type that can be used for many different things but it just is a, a common thing for me use in my day to day activities. And so I become very, very comfortable with it, with any programming language that I get into, because I know I'm gonna wind up using it quite a bit. So a dictionary is really nothing more than a collection of key value pairs and all that means, is that for every value that I want to put into a dictionary, it's going to have a unique key that represents it that's going to allow me to easily access that value. So you can think of it as really a, an actual dictionary of words where the key is the actual word that you're looking up and its value is the definition and you can only have a single entry for each word within the dictionary to represent a value. And you can have common values amongst the different keys within your dictionary but all the keys always have to be unique. So, let's go ahead and see what this looks like within Swift. So now obviously the first thing you're gonna want to be able to do is to create a new instance of a dictionary. There's a couple different ways to do that. So I can say dictionary is going to be equal to. And I can once again use the dictionary keyword or type to define a dictionary using or getting access to its initializer. And once again, dictionary is a generic type that actually has two generic arguments. The first one being the type that is going to represent the key. And the second one being the type that's going to represent the value. So in our case, we're going to specify, well you could put anything you want in here. Let's say that the key is going to be a string and that the value is going to be an integer. So as you can see here, we're creating a simple dictionary, and right now there's actually nothing in it, but we'll go ahead and see how we're going to take care of that in a few moments. So this is going to create a new instance of an empty dictionary. As you can see here, we have zero key value pairs. Now, there's a shorthand notation that you can use to actually create these dictionaries as well. So instead I could say something like this. I could say my little dictionary here is going to be equal to, and then I can use these square brackets, and within here I can specify the, the types of the keys and values just like I did before, up here. And in this case I'm going to say string and we're going to separate them with a colon so I can say string int and then open close parentheses. And there you have it, I have initialized my dictionary to a dictionary that has strings and ints. So there you go, now we've been able to create a very simple dictionary that just so happens to be empty at this point. So let's go ahead and make this a little bit snazzier and actually get some data in it. So if I wanted to create a dictionary and then ultimately initialize it with some predefined data. I can do that simply by specifying that my dictionary is going to be equal to, and then once again we're going to use the open close square brackets. And then within here we're going to specify name value pairs that are comma separated. So let's go ahead and do that now. We'll say that one, the string one is going to represent and we're gonna specify a colon here to separate the key value pairs once again as we're gonna specify the integer one. And then we're going to have the key two is going to reference the numeral 2 and then so on and so forth. We could continue to do this all day long if we wanted, so we'll just create four in here, making sure that we separate each one of the key value pairs by a comma. So as you can see here now, I've initialized my dictionary to four key value pairs that are containing the strings one, two, three, and four that ultimately have the values of the integers one, two, three and four. So once we get values into our dictionary, how do we get them out? Well, it's very simple. In order to get the values out, we're going to use a very common index type notation to be able to get access to a particular value that's going to allow us to get data out and actually update it as well. So if I wanted to get the first value out, I could say VAR first, and I want to set this equal to my dictionary, and I'm going to use that index notation so I'm going to specify the open and closed square brackets. And then I'm going to specify the key that I want to retrieve the value for so in our case we're going to say I want to get a value associated with the key one. So if we take a look over here, you're gonna see that returned an optional sum one and if you are unfamiliar with optionals go ahead and go back a few lessons, and kind of refresh your memory as far as what optionals are and how they work. But just keep in mind that when I'm specifying a, an indexer or like this where I'm specifying that I want to retrieve a value associated with a particular key in my dictionary. There's no given that I'm going to actually specify a key that exists. So if I specify a key that does exist, I'm going to get a, an optional here of sum and then whatever the value of that particular or whatever value is associated with that particular key. But, if I were to come down here and specify that I want to get the value associated with a key that does not exist, I'm going to get a nill. Okay. So now I've actually retrieved the, the optional value associated with this particular key. I can start to work with it like I would normally work with any other optional. Now if I wanted to overwrite the value that was actually in here that's associated with that key I could simply access this once again the same way, by its key and then I could now set this to something else, say 100. So, now as you can see here, my dictionary has a key of one, but now the value associated with the string one, as you can see here, is 100. So I can use this same notation for both read operations and write operations. So, that's fairly powerful, and a very interesting thing to be able to know, and to be able to use, when getting basic access to your dictionary. There's also some helper methods that are associated with the dictionary, as well. So, let's say that I wanted to, maybe, update a value in, in the dictionary. Instead of using the shorthand notation, there are some helper methods. So, I could say, I want to update a value, and I wanna specify the value that I'm associating. So in here I will say that I want to update to a value of one for the key one. So that's just gonna work in a similar fashion, so if I were to go and check out the dictionary value now, you can see that I have now set the value associated with the key one or the string one to be the integer one. So that's a nice little helper method that's going to help you update a value in a very systematic way. Now, an interesting feature of that as well is if I were to say I want to update a value, and I want a value a five for the key five. It's actually going to return a nill, because at the time of us executing this, this particular key did not exist. But, if I were to now take a look at the values, the key value pairs within dictionary. You can see now that because it did not exist, it actually inserted it for us. So, that's a nice little handy feature to have, but that's kind of a side effect of it not actually having that key value pair there. How do I actually remove a full key value pair out of my dictionary? Well, that's quite simple as well. I can simply say that I want to go into my dictionary and I want to remove either at index or remove a value for a key. So in our case, I want to remove the value that's associated with the key five. And as you can see here, it kind of pops it off and remo, it actually returns that value. So if I were to take a look at dictionary again, we're back down to what we started with. One, two, three, and four. Now another common operation that you're gonna find yourself doing quite often if you are working with dictionaries, especially if you want to do some operations across all of them is you're going to have to figure out how many key value pairs are in there and what those keys are and then being able to access their values. So there's a couple different operations, and a couple different ways that you can do that. The first thing you're gonna wanna know is how many values are there within my dictionary? If I wanted to say, loop through all of the key value pairs and do something with them. Well, I can go ahead and access the count property here. And this is going to return how many key value pairs I have. So that's pretty handy, so I can create a four loop and I could go from some value, up to, another up to, count minus one maybe and access all the different, all the different key value pairs in there. But since the dictionary typically works better and is more optimal when you're retrieving and setting values based on these keys like we did in these operations up here when we were retrieving and setting values within the dictionary. Wouldn't it be nice if we could maybe get access to all the keys, or maybe all the values that are found within the dictionary. And we can definitely do that. And we do that by accessing the keys property that is found on the dictionary. And this is going to return back a collection of all of the keys found within this dictionary. So if you wanted to see this in action. I could create a for loop, and I could say for key in dictionary.keys, and within here I could simply do a print line, and specify what the key is. So now you can see that this ran four times, and I could go ahead and take a look at the values that are associated. And as you can see, the operation print line key actually returned four, one, three, and two. So as you can see by using this keys collection, I'm going to get access to all the keys found within here. And I could actually, even more interestingly, print the values associated with all of the keys found within my dictionary. And, of course, as you recall, this actually, this operation here returns an optional. And if I really wanted to get all of the values and say yes, I know, but I want you to actually give me the value, not the optional, I could specify an exclamation point after the optional. And it's just going to give me the value, not the whole sum and the value, just the value. But you could leave that off, if you want. If you're unsure as to what the types are in there. If you're gonna have any problems with those optionals, and merely get the optionals and their values. So you can the similar type of operation if you want to take a look at all the values that are defined within there. And as you may already have figured out, you can specify the values property to do the exact same thing. So I could say for value in my dictionary.values. And I could go ahead and do something along the same lines if I wanted to do a print and maybe print out all of the values or something along those lines. So as you can see, I am printing out all of my keys, all of my values. So those are some of the basic operations that you're going to be needing when you're using these dictionaries to be able to create them in some sort of manner if you want, whatever sort of short hand you'd like to use or maybe initializing. But then ultimately getting into the dictionary and getting out of the dictionary is fairly easily done by using these kinda shorthand notation or maybe using an update value to insert or something along those lines. But there's a lot of very nice full-featured functionality built in for you. So you don't have to feel like you have to reinvent the wheel when it comes to working with dictionaries within the Swift standard library.

Back to the top