@State dla posiadanych obiektów
Zarządzaj obserwowalnym modelem za pomocą @State
@State dla posiadanych obiektów to bezpłatna lekcja SwiftUI Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej SwiftUI Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. ✨
Często zadawane pytania
Czy lekcja „@State dla posiadanych obiektów” jest bezpłatna?
Tak — pełny tekst „@State dla posiadanych obiektów” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu SwiftUI Academy, przejdź na CoddyKit PRO. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „@State dla posiadanych obiektów”?
Zarządzaj obserwowalnym modelem za pomocą @State Ćwiczysz SwiftUI Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć SwiftUI Academy?
Nie wymagamy żadnego doświadczenia. SwiftUI Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.
Ile czasu zajmuje lekcja „@State dla posiadanych obiektów”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji SwiftUI Academy?
Tak. Każda lekcja SwiftUI Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Od stanu widoku do klasy modelu
- Makro @Observable
- @State dla posiadanych obiektów
- Udostępnianie modeli przez @Environment