Follow along at https://www.hackingwithswift.com/100/swiftui/53.
This day covers Part One of Project 11: Bookworm
in the 100 Days of SwiftUI Challenge.
It focuses on several specific topics:
- Bookworm: Introduction
- Creating a custom component with @Binding
- Using size classes with AnyView type erasure
- How to combine Core Data and SwiftUI
From the description:
In this project we’re going to build an app to track which books you’ve read and what you thought of them, and it’s going to follow a similar theme to project 10: let’s take all the skills you’ve already mastered, then add some bonus new skills that take them all to a new level.
This time you’re going to meet Core Data, which is Apple’s battle-hardened framework for working with databases. This project will serve as an introduction for Core Data, but we’ll be going into much more detail soon.
At the same time, we’re also going to build our first custom user interface component – a star rating widget where the user can tap to leave a score for each book. This will mean introducing you to another property wrapper, called @Binding
@Binding
s are sort of like inout
inside of a functions parameters: It means that some other context owns this value -- but we have carte blanche to mutate it if we want. The Toggle
control is a perfect example of how this can be utilized.
AnyView
can be used to overcome situations where different conditional logic might lead to different types being returned from a View
's body. Better yet, we can use Group
to overcome the performance hit that AnyView
incurs 😀.
Historically, there's always been somewhat of an art to passing around Core Data's "Managed Object Context" and using it to update our app's data model. SwiftUI standardizes this by exposing it in the Environment
:
@Environment(\.managedObjectContext) var moc
Property wrappers FTW 💪