SwiftPumpkin

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

RecomputeRenderEvents mutate stateStateSource of truthbodyUI = f(state)Rendered UIDiffed by SwiftUI
calls or tellsreports backYou never update views. You change state, and the framework works out what to redraw.
  • Like React, SwiftUI views are cheap value descriptions recomputed from state.
  • There's no imperative label.text = ...
PartOwnsLooks like
StateThe only thing you mutate@State, @Observable
View valueCheap description, recomputed oftenstruct: View
IdentityWhat SwiftUI diffs againstStructural 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 screensViews plus @Observable models, little ceremony. Watch for logic drifting into views as it grows.
Heavy custom collection layouts or editorsUIKit where it's still stronger, bridged in.

Compared

Imperative UIKit vs declarative SwiftUI

UIKitSwiftUI
You updateViewsState
Source of truthOften the view itselfState outside the view
Bug classView out of sync with dataIdentity 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.

30-second answer

Explain Declarative UI (React lineage) in 30 seconds.

SwiftAsked directly

Imperative UIKit vs declarative SwiftUI: what fundamentally changes in how you update the screen?

iOS codingAsked indirectly

A form uses isTyping, isSubmitting and isEmpty flags, and sometimes shows a spinner and an error together. How do you fix it?

iOS codingAsked indirectly

A search field and a results list are siblings. Where does the query live, and how does the field change it?

Read it in the app's source