0PricingLogin
SwiftUI Academy · Lesson

Why Views Need State

Understand why SwiftUI views are immutable.

Views Describe, They Don't Hold

A SwiftUI view is a lightweight description of your UI for one moment. It is recreated constantly, so it can't simply store changing values like a normal object. 📐

Structs Are Immutable

Your views are structs, and SwiftUI treats their stored properties as read-only inside body. You cannot just reassign a property and expect the screen to react.

struct CounterView: View {
    var count = 0
    var body: some View {
        Text("Count: \(count)")
    }
}

All lessons in this course

  1. Why Views Need State
  2. Declaring @State Properties
  3. Building a Tap Counter
  4. Toggling Visibility with State
← Back to SwiftUI Academy