0Pricing
SwiftUI Academy · Lesson

@State for Owned Objects

Own an observable model with @State.

@State for Owned Objects is a free SwiftUI Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Who Owns the Model

An observable model needs an owner: a view that creates it and keeps it alive for as long as the screen exists.

@State Owns the Instance

Use @State to let a view own an observable model. The view creates it once and holds onto it across redraws.

@State private var model = Counter()

Why @State Here

Without @State, a fresh model would be made on every redraw, wiping your data. @State persists the same instance.

Reading the Model

Read tracked properties straight from the model in your body, and the view refreshes when they change.

Text("Count: \(model.count)")

Calling Model Methods

Trigger behaviour by calling the model methods from your controls, keeping the view free of the actual logic.

Button("Add") { model.increment() }

Mark It private

Keep owned state private so other types cannot reach in and mutate the model behind the view back.

@State private var model = Counter()

No $ to Read

To read an observable model you use it directly. The dollar sign $ is only for making a binding, which we cover later.

A Full Owned View

Here a view owns the model, reads its value, and updates it on tap, all in one tidy place.

struct CounterView: View {
    @State private var model = Counter()
    var body: some View {
        Button("\(model.count)") { model.increment() }
    }
}

Survives Redraws

Because @State is managed by SwiftUI, your model survives every body re-evaluation while the view is on screen.

One Owner Rule

A model should have a single owner. Child views receive access instead of each making their own copy.

Lifetime Tied to the View

The model lives as long as its owning view. When the view leaves the screen for good, the model is released.

Quick Check

Let us confirm how ownership works.

Recap

You used @State to let a view own its observable model, so a single instance survives redraws while the view reads and updates it. ✨

Frequently asked questions

Is the “@State for Owned Objects” lesson free?

Yes — the full text of “@State for Owned Objects” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.

What will I learn in “@State for Owned Objects”?

Own an observable model with @State. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start SwiftUI Academy?

No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “@State for Owned Objects” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this SwiftUI Academy lesson?

Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. From View State to a Model Class
  2. The @Observable Macro
  3. @State for Owned Objects
  4. Sharing Models via @Environment
← Back to SwiftUI Academy