0Pricing
SwiftUI Academy · Lesson

The @Observable Macro

Make a class observable so views track it.

The @Observable Macro is a free SwiftUI Academy lesson on CoddyKit — lesson 2 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.

The Problem to Solve

A plain model class holds data, but SwiftUI will not redraw when that data changes. We need to make the class observable.

Meet @Observable

The @Observable macro marks a class so SwiftUI watches its properties and refreshes any view that reads them. ✨

@Observable
class Counter {
    var count = 0
}

What a Macro Does

A macro generates extra code for you at compile time, so one short attribute adds all the change-tracking plumbing.

Importing Observation

The macro lives in the Observation framework, so add an import at the top of the file before you use it.

import Observation

@Observable
class Counter { var count = 0 }

No Wrappers Needed

With @Observable you mark plain stored properties. No per-property wrapper is required for SwiftUI to track them.

@Observable
class Counter {
    var count = 0
    var name = "Taps"
}

Reading Triggers Tracking

SwiftUI only refreshes for the properties a view actually reads in its body, so unused values cost you nothing.

Writing Triggers Updates

When you change a tracked property, every view that reads it is automatically scheduled to redraw.

model.count += 1  // views reading count refresh

Computed Properties Work Too

A computed property built from tracked values also updates views, since reading it reads its inputs.

@Observable
class Counter {
    var count = 0
    var label: String { "Taps: \(count)" }
}

Granular by Default

Tracking is granular: changing one property only refreshes views that read that property, not the whole screen.

Replacing ObservableObject

@Observable is the modern replacement for the older ObservableObject plus @Published pattern, with far less boilerplate.

Ignoring a Property

Mark a value with @ObservationIgnored when it should not trigger updates, like a cache or internal scratch value.

@Observable
class Counter {
    @ObservationIgnored var cache = 0
}

Quick Check

Time to check your grip on @Observable.

Recap

You made a model @Observable, so SwiftUI tracks the properties your views read and redraws them precisely when those values change. 🎉

Frequently asked questions

Is the “The @Observable Macro” lesson free?

Yes — the full text of “The @Observable Macro” 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 “The @Observable Macro”?

Make a class observable so views track it. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The @Observable Macro” 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