WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Project 8, part 2 "[String: Astronaut]" WHY

Forums > 100 Days of SwiftUI

@xeutv  

im lost here, both astronaut and mission are structs with some variables inside, but when refering to astronauts Paul uses [String: Astronaut] instead of [Astronaut]. Why is this???

struct ContentView: View { let astronauts: [String: Astronaut] = Bundle.main.decode("astronauts.json") let missions: [Mission] = Bundle.main.decode("missions.json") }

   

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      

@xeutv  

hey @Obelix , thanks for the thorough answer. Silly me I didn't identify it was using Astronauts as a dictionary of astronaut. I am going to continue to see how he uses this in the project so i can understand why he makes this choice.

Thanks again

1      

Thx to both of you. The problem with different kind of Jsons is not new to me. But my question was solved by @xeutv because I simply did not get, that [String: Astronaut] means it is a dictionary 🤣

   

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.