🎉 End-of-year Sale! Save 25% when you subscribe today.

SwiftUI Navigation: Tabs & Alerts, Part 2

Episode #161 • Sep 27, 2021 • Subscriber-Only

We continue our journey exploring navigation with an examination of alerts and action sheets. We’ll compare their original APIs in SwiftUI to the ones that replace them in the SDK that just shipped, and do a domain modeling exercise to recover what was lost.

Previous episode
SwiftUI Navigation: Tabs & Alerts, Part 2
Next episode
Locked

Unlock This Episode

Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.

Sign in with GitHub

Introduction

So this is great, our alert can now be more dynamic based on what is happening in the view.

But there’s a few things that are not ideal about how we have modeled this problem.

First of all, holding both a boolean and an optional to represent an alert leaves us open to holding invalid states. For example, what if the boolean is true yet the optional is nil? That means we are trying to present the alert but don’t have any data to actually show. Or if the boolean is false yet the optional is non-nil. That means we have some data we want to put into an alert, but we aren’t showing an alert.

This improperly modeled domain will add more and more complexity into your view because you can never really trust the data. You’ll always worry about the invalid states creeping up, causing you to sprinkle little bits of logic to handle them, and you’ll have to do extra work to clean up the state so that it stays as correct as possible. In fact, right now we are never nil-ing out the itemToDelete field, and so if another part of our view checks if its nil for some other part of the feature’s logic we run the risk of making decisions based on malformed data.

So, using the simple binding boolean API for alerts works great for alerts that are static, but it’s not the way to go for alerts that are dynamic. Luckily SwiftUI provides another version of the .alert modifier, so let’s take a look at that.

Optional alert state


References

Downloads

Get started with our free plan

Our free plan includes 1 subscriber-only episode of your choice, access to 64 free episodes with transcripts and code samples, and weekly updates from our newsletter.

View plans and pricing