UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 42 Further challenges: Looping through a dictionary

Forums > 100 Days of SwiftUI

Hello! Hope everyone's having a great day/evening.

I finished all three challenges for project 8, but I wanted to push myself by tackling the following,

Extra extra challenge #1: Just like how we created a view that shows a list of all missions, create a new view that shows a list of all astronauts.

Extra extra challenge #2: Just like how we merged Codable structs to get crew members from missions, create a list of missions participated by each crew member in AstronautView.

I thought Challenge #1 would be pretty straightforward, but I found out looping through dictionary [String: Astronaut] works totally differently from looping through an array like we did for [Mission]. I read a few posts online and a lot of them suggest I sort the dictionary into an array and list items of that array, but that seems unnatural in this project since we can't even sort the items in our dictionary.

I guess Challenge #2 is difficult for the same reason, since I would have to loop through the astronauts dictionary and search them in the mission array.

I'm sure with practice it'll get easier, but my brain just freezes when working with dictionaries. Any lead in both challenges would be really helpful, thank you!


One thing you can do is sort the keys of the Dictionary and loop through those:

struct KidsView: View {

    //[String: Int] dictionary
    let kids = ["Charlotte": 11, "Shauna": 12, "Mildred": 13,
                "Linton": 12, "Jack": 12, "Sonny": 12]

    var body: some View {
        List {
            //loop through the sorted keys
            ForEach(kids.keys.sorted(), id: \.self) { kid in
                HStack {
                    //this is the key
                    //this is the value
                    //force unwrap is safe since we know
                    //the key exists in the dictionary


@rooster gives a great answer.

And he also provides a terrific snippet for you to paste into your 100 Days Playground file. Create a new Playgrounds page, name it Dictionary Examples, and write some notes to FutureYou. FutureYou will thank you for thinking ahead. I hope, by now, you have a large collection of snippet pages in Playgrounds, all with appropriate names, allowing you to quickly search, and execute working code snippets.

But please note, another programmer @greenis is on Day 19 and is also asking a question about dictionaries.

See->Day 19 Dictionary Question

You're 20 days ahead of @greenis! To help you lock the lessons into the squishy SwiftUI portions of your brain, it might be time for you to start helping others on their journey. Take a moment to review help requests. Jump into Playgrounds and think of a creative solution. If you can explain the answer to yourself, and demonstrate it in code, you can explain it to other programmers.

But the real benefit is you are solidifying the vague concepts in your brain into bona-fide developer confidence!


Thanks @Obelix, will do! I actually created new playground every time I tested something so I have a whole folder named playground with a bunch of playground files named "Closures", "Opaque Types", and etc.. but I will write code in one file from now on!!


Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.