Deklarowanie właściwości @State
Proszę dodać @State, aby przechowywać zmienne dane lokalne widoku.
Deklarowanie właściwości @State to bezpłatna lekcja SwiftUI Academy na CoddyKit. To lekcja 2 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.
Meet the @State Wrapper
You declare mutable view-local data by marking a property with @State. This property wrapper tells SwiftUI to store and watch the value for you. 🏷️
@State private var count = 0Always Give It a Default
A @State property needs an initial value right where you declare it. SwiftUI uses it to create the storage the first time the view appears.
@State private var isOn = falseMark It private
State belongs to one view, so declare it private. This signals that no outside code should reach in and set it directly, keeping ownership clear.
@State private var name = ""Reading the Value
Read a @State property like any normal variable. Just use its name in body and SwiftUI tracks that this view depends on it.
Text("You tapped \(count) times")Writing the Value
You can mutate a @State property even though the view is a struct. Assign to it from an action and SwiftUI schedules a redraw automatically.
Button("Add") { count += 1 }It Persists Across Redraws
SwiftUI keeps the @State value alive across the many times the view body is recomputed. Your count won't reset just because the view redrew.
Putting It Together
Declare state, read it in the body, and mutate it from a button. That is the full loop that powers most simple interactions.
struct CounterView: View {
@State private var count = 0
var body: some View {
Button("Count \(count)") { count += 1 }
}
}Use Simple Value Types
@State works best with value types like Int, Bool, String, or small structs. For shared objects you'll later reach for other tools, but values are the norm here.
One Owner Per Property
The view that declares @State is its owner. Other views can receive access, but only one view should hold the original state for a given value.
Don't Overuse It
Reach for @State only when a value truly changes while the view is on screen. Constant labels and passed-in data don't need it.
Naming for Clarity
Name state for what it represents, like isLoading or selectedTab. Clear names make the data flow obvious to anyone reading the view.
Quick Check
Which line correctly declares a private piece of view-local state?
Recap
Declare changing view data with @State, give it a default, and keep it private. Read it freely in body and mutate it from actions to trigger redraws. ✅
Często zadawane pytania
Czy lekcja „Deklarowanie właściwości @State” jest bezpłatna?
Tak — pełny tekst „Deklarowanie właściwości @State” 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 „Deklarowanie właściwości @State”?
Proszę dodać @State, aby przechowywać zmienne dane lokalne widoku. Ć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 2 z 4.
Ile czasu zajmuje lekcja „Deklarowanie właściwości @State”?
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
- Dlaczego widoki potrzebują stanu
- Deklarowanie właściwości @State
- Budowanie licznika dotknięć
- Przełączanie widoczności za pomocą stanu