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

SOLVED: Day 41: Why is [String: Astronaut] necessary?

Forums > 100 Days of SwiftUI

I'm a little confused as to why the Astronaut struct is being used as a [String: Astronaut] dictionary in the custom initializer. Whereas the Mission struct is simply used as a [Mission] array.

The code works, and I understand the purpose of usinf a custom init(), but I'm hung up on the difference between these two and how they're being used in the init().


This one is probably one for @roosterboy

If you look at the json files they are different. The Astronaut file start with { then a String followed by { with other field set in struct Astronaut so to decode [String: Astronaut]

    "grissom": {
      "id": "grissom",
      "name": "Virgil I. \"Gus\" Grissom",
      "description": "Virgil Ivan \"Gus\" Grissom (April 3, 1926 – January 27, 1967) was one of the seven original National Aeronautics and Space Administration's Project Mercury astronauts, and the first of the Mercury Seven to die. He was also a Project Gemini and an Apollo program astronaut. Grissom was the second American to fly in space, and the first member of the NASA Astronaut Corps to fly in space twice.\n\nIn addition, Grissom was a World War II and Korean War veteran, U.S. Air Force test pilot, and a mechanical engineer. He was a recipient of the Distinguished Flying Cross, and the Air Medal with an oak leaf cluster, a two-time recipient of the NASA Distinguished Service Medal, and, posthumously, the Congressional Space Medal of Honor."
  // ......

where the Mission is start with [ then the struct Mission so that can be a array of Missions so to decode use [Mission]

        "id": 1,
        "crew": [
                "name": "grissom",
                "role": "Command Pilot"
            // .......


@nigel has the right answer. But allow me to add a different way to explain it?

Index Cards or 3x5 Cards

Grab a dozen or so index cards. On half of them, very meticulously copy the information for a few missions. Mix them up and put them face down on your desk.


Mission Name
Mission ID
Mission Description
Crew list
Launch Date

On a few other cards, do the same, but for astronauts. Add astronaut info onto one side of the card.

Military Rank
Favorite Pasta Dish

Array vs Dictionary

Now, and this is the important part, WRITE the astronaut's name on the BACK of their index card.

Flip all the cards face down on your desk into two piles, one for astronauts and one for missions.

Notice the difference? You have six or so missions on your desk, but you can't see the data directly. Think of these cards as an array. Likewise, you can't see the astronaut data directly, but you CAN SEE one key bit of information...their name!

Put your finger on mission card #3. It might be Apollo 14. Or maybe not. You can't infer the mission's data from the array's index. Put your finger on the card with the Mitchel key. Take a guess who's data is on the other side.

Find particular Missions

If you're given the task to find all missions flown in 1969, you'd have to flip each card, one at a time, and look at each card's mission date. If the mission date matches your criteria, you then copy that card.

I just described an array's filter function!

// Filter an array to find missions launched in 1969
// flip each 'card' one at a time, compare its launchDate
let foundMissions🚀 = missions.filter { $0.launchDate.contains("1969") }   // pseudo code to filter missions.

Find an astronaut

If you're given the task to find all the astronauts who flew on that 1969 mission, you might consider writing a similar filter. However, the astronauts are on your desk and have key values available for you.

The mission card might tell you that armstrong was the mission commander. Find the armstrong card! It's easy because you have the key and you can go directly to the 3x5 card named armstrong. His bio and photo will be on the back of that card.

// Find the armstrong index card
let apollo11Commander👩🏼‍🚀 = astronauts['armstrong']  // flip over the card with armstrong written on the back!

Array vs Dictionary

This is a tough lesson for many on the 100 Days path. But it shouldn't be so confusing. @twoStraws is teaching two lessons here.

First, your JSON data may be described as a list of separate objects, as he's done with the Missions. Sometimes, you'll run across JSON data that may look like separate objects, but are arranged as dictionaries. This is the case for astronauts. You'll run into both whilst writing Swift code.

Keep Coding

Which should you use in your solution? Straight up arrays? or Dictionaries? You're in the command pilot seat. That decision is up to you.


TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

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.