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.