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().

1      

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"
            },
            // .......

2      

@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.

Add:

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.

Name
Biography
Military Rank
Photo
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.

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.