In SwiftUI, why is a view a lightweight struct whose body SwiftUI reads repeatedly, rather than a long-lived object you update?
answer
- description, not a screen object
- the View protocol's one requirement
- body read again on updates
- recreated often, so init stays light
basics
~20 sA SwiftUI view is a value describing what to show for its current inputs. SwiftUI reads its body whenever an update needs it and redraws only the affected parts, so view structs are recreated often and must stay cheap.
solid answer
~40 sA type conforms to `View` by providing a `body` computed property, and that body is a *description* of the UI for the view's current inputs, not a handle on something drawn. SwiftUI reads `body` any time it needs to update — typically after user input or a data change — and redraws only the parts of the interface that changed, so the framework, not my code, does the add, remove and update work. Because the system may reinitialize a view struct at any time, I keep initializers trivial and avoid significant work there; real state lives in storage SwiftUI manages for `@State`, or in models, not in the struct's plain properties. The protocol is also `@MainActor`, so `body` is read on the main actor.
code
swift · 25 linesimport SwiftUI
struct TemperatureLabel: View {
let city: String
let celsius: Int
var body: some View {
VStack(alignment: .leading) {
Text(city)
.font(.headline)
Text("\(celsius)°")
.font(.largeTitle)
}
}
}
struct CityRow: View {
let celsius: Int
var body: some View {
// A new TemperatureLabel value is created on every read of this body;
// that is expected and cheap.
TemperatureLabel(city: "Lisbon", celsius: celsius)
}
}go deeper
Recall that a view is a struct conforming to View with one required body property, and that body describes the UI for the current inputs rather than drawing it.
Explain that SwiftUI reads body again when inputs change, may reinitialize the struct at any time, and keeps state storage outside the struct, so init and body stay light.
Turn the model into review rules: side-effect-free bodies, no heavy work in init, narrow inputs per subview, and state kept in SwiftUI-managed storage or models rather than plain properties.
Present the value-description model as what lets the framework own update work, and say how that shapes a team's review checklist and the onboarding of engineers used to mutating view objects.
## What a SwiftUI view is In SwiftUI, a **view** is any type that conforms to the `View` protocol. The protocol's main requirement is a `body` computed property, and Apple's documentation describes what that property holds: each view contains **a description of what to display for a given state**. A view is therefore not a box drawn on screen that you keep and adjust; it is a small value — almost always a `struct` — whose stored properties are its inputs and whose `body` turns those inputs into more views. The `body` property is declared as `some View`. That tells Swift the property returns one concrete view type without spelling it out; the compiler infers it from whatever the body contains. Built-in views such as `Text`, `Image`, `VStack` and `ProgressView` are views in exactly the same sense, so a screen is a tree of small descriptions nested inside each other. The protocol is also declared `@MainActor`. A type that declares its `View` conformance in its original declaration inherits that isolation, so a view's `body` is read on the main actor. ## How SwiftUI uses body The documented cycle is short: 1. You declare the hierarchy: a root view whose `body` contains other views, configured with modifiers and given their inputs. 2. Something changes — the user taps, a model publishes new data, a parent passes a different value. 3. SwiftUI notices which inputs changed and reads `body` again where it needs to. Apple's documentation says it reads the property "any time it needs to update the view, which can happen repeatedly during the life of the view". 4. SwiftUI **redraws only the affected parts** of the interface. Doing that may involve reinitializing the view struct, and SwiftUI manages that for you. Modifiers follow the same idea. Calling `.font(.title)` or `.opacity(0.5)` does not change a property on an existing object; it returns a **new view that wraps the original** and takes its place in the hierarchy. ## Why a struct and not an object - **Cheap to create.** A struct with a couple of `let` properties costs almost nothing to build, so SwiftUI can create it as often as updates require. - **No hidden mutable state.** A plain stored property cannot drift out of sync with the screen, because the screen is recomputed from the value rather than patched by hand. - **Identity is not the struct's job.** A value has no reference identity of its own; SwiftUI decides whether two descriptions are the same view from their position in the tree or an explicit ID, which is the subject of the lesson on view identity. - **Main-actor reads.** Because `View` is `@MainActor`, `body` is read on the main actor, where UI updates happen. ## Who owns what | Piece | Owner | Lifetime | |---|---|---| | The view struct | Your code | Recreated whenever SwiftUI needs it | | What is drawn on screen | SwiftUI | Updated in place where inputs changed | | `@State` storage | SwiftUI manages it | Kept while the view keeps its identity | | Models and services | Your app | However long your app keeps them | The table explains the most common surprise for newcomers: a plain `var` on a view struct is not state. `body` is a non-mutating getter, so a button action inside it cannot assign to a stored property — the compiler rejects it — and any value you did manage to change would be replaced the next time the parent recreates the struct. View-owned values live in `@State`, whose storage SwiftUI manages outside the struct; how the state wrappers work belongs to the lessons on state. ## Rules that follow from the model - **Keep initializers trivial.** Apple's documentation warns that the system may reinitialize a view at any time, so significant work in `init` can run many times. It is often best to let Swift synthesize the memberwise initializer. - **Keep `body` cheap and free of side effects.** It can be read many times; starting network calls or heavy formatting there repeats them. Work that should run when a view appears belongs to the lesson on appearance and async work, and measuring redraws belongs to the performance lesson. - **Pass narrow inputs.** A subview that receives only the values it shows gives SwiftUI a precise picture of what changed. - **Extract subviews freely.** Splitting a long `body` into small view structs costs little, because the structs themselves are cheap values. ## How to answer in an interview Say that a SwiftUI view is a value describing UI for its current inputs, that SwiftUI reads `body` whenever an update needs it and updates only what changed, and that this is why views are structs, why initializers stay light, and why real state lives in storage SwiftUI manages rather than in the struct.
- Can you keep a tap counter in a plain var on a SwiftUI view and increment it from a Button?No. `body` is a non-mutating getter of a struct, so assigning to a plain stored property from a closure inside it doesn't compile, and a changed value would not survive the parent recreating the struct anyway. A view-owned value goes in `@State`, whose storage SwiftUI manages outside the struct and keeps for as long as the view keeps its identity.
- Does SwiftUI re-read every view's body in the app whenever any data changes?No. Apple's documentation says SwiftUI notices which input changed and redraws only the affected parts of the interface. That is why small subviews with narrow inputs help: a subview whose inputs and dependencies did not change gives SwiftUI no reason to update it. Measuring how often bodies actually run is a profiling task covered with SwiftUI performance.
A view is like an order ticket handed to a kitchen: when the order changes you write a fresh ticket, and the kitchen works out which dishes actually need changing instead of cooking the whole meal again.
- A SwiftUI view struct is the object drawn on screen and lives while visible.
- Body runs once when the view first appears, like a load callback.
- Expensive setup belongs in a view's init because it runs only once.
- Changing a plain var on the view struct updates the screen.
- Every data change makes SwiftUI rebuild the whole window from scratch.