0Pricing
SwiftUI Academy · Lezione

@State per gli oggetti posseduti

Gestisca un modello osservabile con @State.

@State per gli oggetti posseduti è una lezione SwiftUI Academy gratuita su CoddyKit. Questa è la lezione 3 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.

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. ✨

Domande Frequenti

La lezione «@State per gli oggetti posseduti» è gratuita?

Sì — il testo completo di «@State per gli oggetti posseduti» è 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 «@State per gli oggetti posseduti»?

Gestisca un modello osservabile con @State. 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 3 di 4.

Quanto tempo richiede la lezione «@State per gli oggetti posseduti»?

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

  1. Dallo stato della vista a una classe modello
  2. La macro @Observable
  3. @State per gli oggetti posseduti
  4. Condividere i modelli tramite @Environment
← Torna a SwiftUI Academy