8.1 · What a View is, and how SwiftUI knows which view is which

A SwiftUI view is a cheap description of the interface, not an object on screen. The framework keeps its own long-lived state and uses identity to decide which description is the same view as before.

Mental model. You write a value that describes the screen. SwiftUI compares this description with the last one, by identity, and updates only what changed. State belongs to an identity, not to your struct.

Views as values

The protocol, <code>some View</code> and modifiers.

  1. What is a View, what does some View mean, and why do modifiers wrap rather than mutate? 8.1.1 · L1
    A View is a protocol with one requirement: var body: some View. Conforming types are usually small structs, and creating one is cheap: it is a description, not…

Identity

Structural and explicit identity, and what resets state.

  1. What is structural versus explicit identity in SwiftUI, and what happens to state when identity changes? 8.1.2 · L3
    SwiftUI needs to know, between two evaluations of body, whether a view is the same view (keep its state, animate the change) or a new one (create fresh state…

More in SwiftUI