100 Days of SwiftUI

Day 52

2026-06-25

Project 10, part 4

Cupcake Corner: Wrap up

Quiz: 10/12 whomp-whomp

Challenge

  1. Our address fields are currently considered valid if they contain anything, even if it’s just only whitespace. Improve the validation to make sure a string of pure whitespace is invalid.

My thought to use USPS API for validation got stopped in its tracks because USPS’s website doesn’t like me. Plus, it’s really fiddly for a one-off so I gave up.

More likely it doesn’t like Safari, or old cookies or … it worked in Firefox

The quick solution as stated is to replace the name.isEmpty from the checker with name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty (for all 4 fields)

  1. If our call to placeOrder() fails – for example if there is no internet connection – show an informative alert for the user. To test this, try commenting out the request.httpMethod = "POST" line in your code, which should force the request to fail.

I added @State private var confirmationTitle = "" and then adjusted the alert to show the title of the confirmation instead of fixed text. It’s really difficult to simulate “no network”! I had to turn off my mac’s network to do it.

  1. For a more challenging task, try updating the Order class so it saves data such as the user’s delivery address to UserDefaults. This takes a little thinking, because @AppStorage won’t work here, and you’ll find getters and setters cause problems with Codable support. Can you find a middle ground?

So my tricksy way of thinking about this is to get the user data from the phone. But it is also too fiddly for my brain today. I’m thinking that it would be a subset of the existing Order JSON, saved to UserDefaults. Maybe I’ll do this after I’ve had a nap.