Programming in Swift: Fundamentals

Oct 19 2021 · Swift 5.5, iOS 15, Xcode 13

Part 4: More Collections

29. Accessing & Working with Dictionaries

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 28. Creating & Populating Dictionaries Next episode: 30. Challenge: 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.

Learn more Already a subscriber? Sign in.

Notes: 29. Accessing & Working with Dictionaries

Apple’s Swift Dictionary documentation: https://developer.apple.com/documentation/swift/dictionary

Update Notes: The student materials have been reviewed and are updated as of October 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Hey, welcome back in the previous video, you learned how to create dictionaries, how key value storage works, and how to add an update elements to your dictionary. But there's a whole lot more to dictionaries than just that. Now dictionaries have many of the same qualities as arrays. You can use their built-in methods to check, to see if they're empty, to see how many key value pairs they contain, or even to see if a dictionary contains a particular value, or after. But unlike arrays, dictionaries are a bit more flexible, when you ask a dictionary for something it doesn't contain. Do you remember what happened when you tried to access, an array index that didn't actually exist? That's right. You got the dreaded index out of range error. You always had to be aware of the range, of a particular array, but with dictionaries, all you have to do is ask it for a key. And if it doesn't exist, a dictionary simply returns, nil, easy peasy. There's no error and there's no ranges to keep track of. And just like arrays, you can iterate over dictionaries with a loop, but instead of just returning, the value of the element of a particular index in the array, a dictionary returns that key value pair, inside a tiny collection, that you saw earlier in this course. That's right. A Tuple. Now open up the playground page for this video, and let's get started learning how to access and iterate over dictionaries. To access a value in a dictionary. You can use the same syntax, you use to access values in an array, this time though, you use a key instead of an index number. So to get Rinse wind's pet, (keyboard typing) You just put Rinse wind and brackets like that. And unlike arrays, if you try to get a value for key, that doesn't exist, (keyboard typing) You get nil back instead of a fatal error. So Captain Ahab, isn't a key in our dictionary. So he get nil back. You can check that result just like you would any optional. For example, you can use nil coalescing like this. (keyboard typing) (keyboard typing continues) So there's no Captain Ahab in this dictionary. So you get the default value, of no white whale for captain Ahab in the sidebar. Dictionary has a few other properties, that should look familiar. You can check if a dictionary is empty with 'is Empty'. (keyboard typing) And you can get the count to tell you, how many pairs are in a dictionary. (keyboard typing) We've got seven. Now, removing pairs from dictionaries, is just as easy as adding them. You can use the 'remove Value' method, to remove the value for a specific key. Now Goku dies several times throughout his life. So if we remove the value for Goku. (keyboard typing) Now that removes the entire pair. What's interesting is that remove value, returns the value corresponding to the key, you just removed in case you want to do something, with that value. Now you can't have a key without a value in a dictionary. So remember that if you try to check for a key that doesn't exist, the result is nil. Let's try setting the value for a 'Hiccup' to nil, to see what happens. (keyboard typing) And I'll print out the dictionary to see the result (keyboard typing) In the console. You can see that both Goku and Hiccup have been removed. You can iterate through arrays, using four loops, and you can do exactly the same thing with the dictionary. The difference is that you need to account for having a key and a value, instead of just a value. If you want to use both the key and the value inside of your loop, you can name both of them in parentheses like this. (keyboard typing) And then you can try printing something, from inside the loop, such as character has a pet (keyboard typing) And in the console you can see who has what kind of pet. Now, if you only want to use the key, or only the value inside your loop, there are a couple of ways you can do that. And you might have guessed one of those already. That's right. You can use an underscore in place, as a part of the pair you don't need. Just like when you pull values out of a tuple. (keyboard typing) Now, the other option is to specify, that you want to iterate through just the keys or, just the values of the dictionary. Both keys and values are properties of every dictionary. And swift makes it easy to iterate over either. Let me put a little separator in here. That's to separate the output from the previous command, from what I'm about to do. (keyboard typing) Okay, that's good. Now let me iterate over this dictionary, using the dot keys property. (keyboard typing) This does the exact same thing as it did before. It's just a little more compact and clear. I can also iterate over the values of the dictionary, in the same way. (keyboard typing) You can check the console. To see the list of names and those to pets? Now you can also go to help develop a documentation and look up dictionary and then scroll through. Because just like with the arrays, there are way more properties and methods available than, what I'm able to show you. just stay curious as you continue programming and swift, and don't be afraid to try new things. That's how you learn now. That's it for this video, it's time to exercise your knowledge of dictionaries, in the next video, where I have some challenges for you. I'll see you there.