La macro @Observable
Renda osservabile una classe, così che le viste possano tenerne traccia.
La macro @Observable è una lezione SwiftUI Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento SwiftUI Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso SwiftUI Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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 refreshComputed 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. 🎉
Domande Frequenti
La lezione «La macro @Observable» è gratuita?
Sì — il testo completo di «La macro @Observable» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso SwiftUI Academy, passa a CoddyKit PRO. Il corso SwiftUI Academy include 4 lezioni in totale.
Cosa imparerò in «La macro @Observable»?
Renda osservabile una classe, così che le viste possano tenerne traccia. Eserciti SwiftUI Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare SwiftUI Academy?
Non è richiesta alcuna esperienza precedente. SwiftUI Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «La macro @Observable»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione SwiftUI Academy?
Sì. Ogni lezione SwiftUI Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Dallo stato della vista a una classe modello
- La macro @Observable
- @State per gli oggetti posseduti
- Condividere i modelli tramite @Environment