|
I'm a little confused as to why the Astronaut struct is being used as a 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 If you look at the json files they are different. The
where the
|
|
@nigel has the right answer. But allow me to add a different way to explain it? Index Cards or 3x5 CardsGrab 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:
On a few other cards, do the same, but for astronauts. Add astronaut info onto one side of the card.
Array vs DictionaryNow, 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 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 Find particular MissionsIf 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
Find an astronautIf 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 The mission card might tell you that
Array vs DictionaryThis 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 CodingWhich 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 more!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.