Unlock This Episode
Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.
So we are now accomplishing everything that we did with the
unwrap binding helper, but in a more general fashion. We can instantly abstract over any case of any enum in order to show case-specific UI controls. As soon as the state flips from one enum case to a different enum our UI will instantly update and we’ll get bindings for the data in that case so that sub views can make changes to the data and have it instantly reflected in our model.
This is incredibly powerful. We have truly unlocked some new functionality in SwiftUI that was previously impossible to see with the tools Apple gave us. Apple simply does not make it easy for us to use enum for state in SwiftUI, and instead all of the tools are geared towards structs.
But, here on Point-Free we know the importance of putting structs and enums on equal footing. Once we have a tool designed for structs we should instantly start looking for how the equivalent tool looks like for enums, and once we have a tool designed for enums we should instantly start looking for how the equivalent tool looks like for structs. And this is what led us to discover a new way to transform bindings, which also led us to a better way to construct our view hierarchy. We can now model our domain exactly as we want, and we can have the view hierarchy naturally fall out of that domain expression rather than creating a bunch of escape hatches to project information out of the domain in an imprecise manner.
In this episode we recreated a technique that Apple uses in one of their SwiftUI code samples. In the sample Apple creates a UI to handle editing a profile with the ability to either save the changes or discard the changes:
In the Landmarks app, users can create a profile to express their personality. To give users the ability to change their profile, you’ll add an edit mode and design the preferences screen.
You’ll work with a variety of common user interface controls for data entry, and update the Landmarks model types whenever the user saves their changes.
Follow the steps to build this project, or download the finished project to explore on your own.
CasePaths is one of our open source projects for bringing the power and ergonomics of key paths to enums.
Enums are one of Swift’s most notable, powerful features, and as Swift developers we love them and are lucky to have them! By contrasting them with their more familiar counterpart, structs, we can learn interesting things about them, unlocking ergonomics and functionality that the Swift language could learn from.