100 Days of SwiftUI

Day 40

2026-05-29

Project 8, part 2

  • Focus on getting the data right
  • Meeting generics
  • getting more comfortable with splitting code out of the main ContentView
  • colors!

Loading a specific kind of Codable data

  • bringing JSON data in; need to consider how to do it in a way that is easy to maintain and doesn’t “clutter our code”
  • moved Decode code out of ContentView into another file
    1. makes ContentView easier to read
    2. gives the possibility to easily reuse the code in another project
      • aided by adding more reasons to throw FatalError

Using generics to load any kind of Codable data

  • you can nest structs to give added context (so instead of talking about Crew, you talk about Mission.Crew)
  • generics allow code to work with a variety of different types (such as a dictionary of astronauts and an array of missions)
  • uses a placeholder, conventionally T in angle brackets (aka “Pulp Fiction brackets”?!) after the func name, e.g. func decode<T>...
  • note that T stands for the type of thing, so if it’s already an array, you don’t need to use [T]
  • when you actually go to use the function you have to tell it what it is going to return tiny sketch of a samoyed face with lolling tongue
John Travolta holding two fingers in a v-shape pointing at the side of his head We call them “chicken beaks”

Formatting our mission view

  • the images we’re using are named apollo1, apollo7... but the id in the Mission struct (and the JSON) is just an integer.
  • adding a “Display name” property to the Mission struct makes it possible to have only one place where you need to develop the name for views.
  • same for formatted dates (so they read correctly in different locales)
  • you can specify app colors with Assets or with an extension. One is better for visual, other is better for version control
  • Color conforms to ShapeStyle, so extend that
    • I did it for the text color also
  • you can set a preferred display mode (.preferredColorScheme(.light) or .dark) so you don’t have to design the colors twice ;)