Skip to main content

Day 42

About 3 minSwiftcrashcoursepaul-hudsonswiftswiftuihacking-with-swiftxcodeappstore

Day 42 ๊ด€๋ จ


100 Days of SwiftUI - Day 42

Project 8, part 4

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
100 Days of SwiftUI - Day 42 - 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.open in new window

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.

  1. 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.
  2. 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 the Rectangle dividers out too.
  3. 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.


์ด์ฐฌํฌ (MarkiiimarK)
Never Stop Learning.