Previous episode: 27. Introduction
Next episode: 29. Accessing & Working with Dictionaries
Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
Okay, so you're ready to learn about another really useful collection type and Swift, the dictionary. Now a dictionary is an unordered collection of pairs where each pair is composed of a key and value. Now, dictionaries are really useful when you want to look up values by means of their identifier. So for example, here we have a dictionary where the keys are the names of the animals, and the values are the images of cute animal icons. Now keys and dictionaries have to be unique. The same key can't appear twice in a dictionary. So we couldn't have two entries for cat, for example. However different keys can point to the same value. So for example, if we had a key for amphibian, it could point to the frog icon. And also all the keys and dictionary must be the same type. For example, strings here. And on the values must be the same type too. Example, the images. Now, how is this different from an array? With an array, you can only fit your value by its index. The index has to be integer and all indexes must be sequential, or in order more. With the dictionary, the keys can be of any type and in no particular order. Now let's try using dictionaries in Swift. You can create an empty dictionary, just like you can create an empty array, the only difference is index. You need to specify the types from both the keys and the values with a colon, right after the key type. And to create an empty dictionary, just put a colon right between a pair of square brackets. (typing) Also like with an array, if you create an empty dictionary, the type annotation is required because there's not enough information for the compiler to infer the type. But if you make a dictionary and add values, right away, type inference will work. For example, let's make a names and pets dictionary and add Ron and his pet rat to it. (typing) So now the compiler can figure out that you want a dictionary with strings for keys and strings for the values too. So I'll keep filling in the dictionary to store some characters and the types of pets they carry around. I'm using emoji here with the control command space, keyboard shortcut, but that can be hard to type so you don't have to, it's just for fun. So now let's try printing out the whole dictionary just to see what it looks like in the console. (typing) Hey, it looks exactly like the declaration of the dictionary keys and values. So if you have an existing dictionary and you want to add key value pairs to that dictionary, it's really easy to do that. Like many tasks since Swift, there's more than one way to accomplish things and dictionaries are no exception. So I'll have myself, Chris, in here and I'll add my pet dog, Mango. So to do that, I can use the update value method on the names and pets dictionary like this. (typing) So I provide the name of the value I want to update or add, Mango. And then the name of the key to add it under. And that's Chris. It might sound strange to use update value to add value, but this is actually an efficiency you'll see a lot in Swift. Instead of having an add value method, you simply call update value. Swift figures out whether to add it or update it, and the end result is the same. There's also a shorthand method to add values to a dictionary and that's what I prefer. Calvin has a pet tiger. So let's add them to our dictionary with the shorthand way of doing things. So I put the name of the dictionary, specify the key in square brackets, and then set that equal to the value I want to add. (typing) It's very much the same way you had elements to arrays, which is why I like it so much. Modifying or updating a dictionary is nearly the same thing as updating a mutable array. You've got some options. So Ron eventually lost his pet rat because it turned out to be a mass murder in disguise. Oh well, but after that he got an owl. Now I can update Ron's pet with the update value method. (typing) There, it's the same syntax as adding a key. That's nice. Oh, wait. I forgot to add in the owl emoji. Oh, that's okay though. I use a shorthand method to update Ron's pet with a wise old owl emoji. (typing) So let me print out the new dictionary. (typing) Run my playground again. There, now I can see that Ron's entry has been updated properly. Now that's the end of this video. Up next I'll show you a few more ways that you can modify dictionaries and how to iterate over dictionaries, just like you did with arrays. I'll see you there.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.