Day 42
Day 42 ๊ด๋ จ
Project 8, part 4
Youโve now finished Moonshot, which is the first of our projects that started to get difficult โ it took longer to explain, we used custom SwiftUI layouts, and I even snuck in a few advanced Swift features. Not all our future projects are going to be difficult, but itโs certainly going to be the case that they are more complex; there is more to them, which means the resulting apps are more interesting, and also more representative of real-world app building.
As the complexity rises so do the odds of making mistakes, and Swift is pretty unforgiving here โ as you will undoubtedly have seen by now, even one small mistake on line 20 can result in a random error appearing on line 5, and this can be disheartening.
Well, I hope todayโs quote will inspire you. Iโve chosen it specially for today for reasons best left to the reader, but itโs this: donโt panic! These sorts of problems are common, and the easiest way to solve them right now is to comment out whatever code you most recently added, and keep doing so until your code works. You can then slowly re-introduce code until you find the part that causes your compile to break, then fix it.
So, donโt panic โ you can do this!
Today you should work through the wrap up chapter for project 8, complete its review, then work through all three of its challenges.
Moonshot: Wrap up
Moonshot: Wrap up
This app is the most complex one weโve built so far. Yes, there are multiple views, but we also strayed away from lists and forms and into our own scrolling layouts, using GeometryReader
to get precise sizes to make the most of our space.
But this was also the most complex Swift code weโve written so far โ generics are an incredibly powerful feature, and once you add in constraints you open up a huge range of functionality that lets you save time while also gaining flexibility.
Youโre also now starting to see how useful Codable
is: its ability to decode a hierarchy of data in one pass is invaluable, which is why itโs central to so many Swift apps.
Review what you learned
Anyone can sit through a tutorial, but it takes actual work to remember what was taught. Itโs my job to make sure you take as much from these tutorials as possible, so Iโve prepared a short review to help you check your learning.
Click here to review what you learned in this project.
Challenge
One of the best ways to learn is to write your own code as often as possible, so here are three ways you should try extending this app to make sure you fully understand whatโs going on.
- Add the launch date to
MissionView
, below the mission badge. You might choose to format this differently given that more space is available, but itโs down to you. - Extract one or two pieces of view code into their own new SwiftUI views โ the horizontal scroll view in
MissionView
is a great candidate, but if you followed my styling then you could also move theRectangle
dividers out too. - For a tough challenge, add a toolbar item to
ContentView
that toggles between showing missions as a grid and as a list.
Tip: For that last one, your best bet is to make all your grid code and all your list code two separate views, and switch between them using an if
condition in ContentView
. You canโt attach SwiftUI modifiers to an if
condition, but you can wrap that condition in a Group
then attach modifiers to there, like this:
Group {
if showingGrid {
GridLayout(astronauts: astronauts, missions: missions)
} else {
ListLayout(astronauts: astronauts, missions: missions)
}
}
.navigationTitle("My Group")
You might hit some speed bumps trying to style your list, because they have a particular look and feel on iOS by default. Try attaching .listStyle(.plain)
to your list, then something like .listRowBackground(Color.darkBackground)
to the contents of your list row โ that should get you a long way towards your goal.
Moonshot: Wrap up