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

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.

1      

@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

   

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.