21 of 22 · SwiftUI architecture
Declarative UI (React lineage)
Declarative UI describes what the interface should look like for a given state, and the framework works out the changes. In SwiftUI, a view is a cheap value whose body is a function of state, the same idea React popularized.
How it's wired
- Like React, SwiftUI views are cheap value descriptions recomputed from state.
- There's no imperative label.text = ...
| Part | Owns | Looks like |
|---|---|---|
| State | The only thing you mutate | @State, @Observable |
| View value | Cheap description, recomputed often | struct: View |
| Identity | What SwiftUI diffs against | Structural identity, .id(_:) |
What matters in real apps
- body runs often. Keep it pure and cheap: no network calls, sorting or formatter allocation inside it.
- Identity drives everything. Structural identity (if/else branches) and explicit identity (.id, ForEach ids) decide whether state is kept or reset. Unstable ids reset state and break animations.
- A view re-evaluates when anything it reads changes. With @Observable that's tracked per property, so read narrowly and pass children the smallest values they need.
- Use UIKit through representables for what SwiftUI lacks, and keep that bridge thin.
When I'd go declarative
| New screens on iOS 17+ | SwiftUI with @Observable. |
|---|---|
| A small team and a handful of screens | Views plus @Observable models, little ceremony. Watch for logic drifting into views as it grows. |
| Heavy custom collection layouts or editors | UIKit where it's still stronger, bridged in. |
Compared
Imperative UIKit vs declarative SwiftUI
| UIKit | SwiftUI | |
|---|---|---|
| You update | Views | State |
| Source of truth | Often the view itself | State outside the view |
| Bug class | View out of sync with data | Identity and state ownership |
Expert notes
- In SwiftUI you don't update views. You update state, and identity decides what survives.
Check yourself
Answer out loud before you reveal. Recall is what makes it stick.
Explain Declarative UI (React lineage) in 30 seconds.
- Declarative UI describes what the screen looks like for a given state, and the framework works out the updates.
- In SwiftUI a view is a cheap value whose body is a function of state, so I update state, never views.
- The nuances are identity, which decides whether state survives, and dependencies: a view re-renders when anything it reads changes, so body must stay pure and cheap.
Imperative UIKit vs declarative SwiftUI: what fundamentally changes in how you update the screen?
- UIKit views are long-lived objects you mutate with commands, like
label.text = name. - SwiftUI views are cheap structs whose
bodydescribes the UI as a function of state. - When state changes,
bodyruns again and produces a new description, and SwiftUI diffs it against the screen, including list insertions and deletions. - Your job shifts from sequencing updates to modeling state.
- That's why the bugs move from forgotten updates to identity and state ownership.
A form uses isTyping, isSubmitting and isEmpty flags, and sometimes shows a spinner and an error together. How do you fix it?
- Replace the booleans with one enum, like
enum Status { case empty, typing, submitting, success, error }. - Independent flags allow impossible combinations, like a spinner plus an error, and an enum makes them unrepresentable.
- Then delete redundant state, since
isEmptycan be computed from the text. - Fewer state variables means new states can't break old ones.
A search field and a results list are siblings. Where does the query live, and how does the field change it?
- Lift the query into the closest common parent as
@State private var query = ""and pass it down. - The field gets
$queryas aBinding, and the list gets the value. - Results are derived, so compute them instead of storing them.
- Data flows down and changes flow up through the binding, which is SwiftUI's version of passing a setter.