Programming in Swift: Fundamentals

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

Part 4: More Collections

30. Challenge: 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: 29. Accessing & Working with Dictionaries Next episode: 31. Working with Sets

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: 30. Challenge: Dictionaries

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.

Okay, it's time for your next challenge, working with dictionaries. Now you can find the challenge in the 04 challenge dictionaries page of the playground you'd be using for this part of the course or you can download a whole new one from the resources for this video. Open it up and try solving the challenge questions on your own first, but then keep watching to compare your work with mine. Good luck. (upbeat music) Okay, so for challenge one, you need to create a dictionary as a variable and initialize it with the following keys. So as part of the initialization you need to assign each of those keys, a value that corresponds to your own personal information, but I'm going to use my information. So I'll start with Var Chris as a dictionary name, and then I'll start creating key-value pairs right in here. I'll say name is Chris. And then my profession is a video tutorialist. My country is Canada. And my city is Halifax. I'll just execute my playground. And if I print out Chris, which is my dictionary and execute my playground again. There, I can see all of my keys and values. On to challenge two. So now that you dictionary's been created, it's time to modify things. Now I've made the bold decision to move to Cleveland, Ohio, USA. So I need to update my information to match. So first I'll update my country to USA. And then I'll update my city to Cleveland. So now I need to add a new key-value to this dictionary to store the state of Ohio, because I didn't have the state key in there before. Now I don't need to do anything special here because adding a new key-value pair, looks just like updating an existing key-value. Done. Challenge three. Alright, so my plans have changed again and I decided to become a digital nomad with no fixed address in the USA. So I need to remove the city and state from my information. So I first need to remove the city key-value pair. I'll use the remove value method for that one and specify this is forKey city. Now I need to use a different strategy to remove the state key-value pair. Remember that a dictionary can't store nil values or keys, so if I simply set the state key to nil and I print out the dictionary. If I execute my playground. There, I can see this all that's left is my country. On the challenge four. So iterate over the remaining keys and values in the dictionary and print them out. So I'll use a four loop and I'll operate on each key-value tubal in my Chris dictionary. Like this. And then I'll print out each pair inside the loop. I'll run my playground again and there we have it. Name is Chris, country's USA, and the profession is video tutorialist. So you'll note that the order in which the key-value pairs are printed out, isn't always the same order that you define them. And that's because by definition, dictionaries are unordered collections. So you've learned quite a bit about dictionaries. In the next video, you'll learn about another powerful collection called sets and how you can use them to perform comparative operations against other sets. I'll see you there.