swiftui · note · architecture
Why SwiftUI views are structs (and why that matters)
Context
SwiftUI views are structs. This is a deliberate design choice.
The snippet
struct MyView: View {
var body: some View {
Text("Hello")
}
}
Why this matters
Structs are cheap to create and discard. They encourage immutability and make recomputation safe.
Views describe what the UI should look like, not how long it should live.
Use it when…
- modeling UI as data
- relying on SwiftUI’s diffing system
- keeping views declarative
Avoid it when…
- assuming views have identity
- storing mutable state in the view itself
Takeaway: Views are values so SwiftUI can rebuild them freely.
Context
In practice