@State a @Binding
Wybierz właściwy sposób zarządzania każdą właściwością
@State a @Binding to bezpłatna lekcja SwiftUI Academy na CoddyKit. To lekcja 4 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.
Two Wrappers, One Goal
Both @State and @Binding manage mutable data, but they play different roles. Picking the right one keeps ownership clear. 🎯
@State Owns
Use @State when this view is the source of truth. It creates and stores the value for itself.
@State private var count = 0@Binding Borrows
Use @Binding when the value lives somewhere else and this view only needs to read and write it.
@Binding var count: IntWho Creates the Value
@State initializes a value. @Binding never does; it always receives one from a parent that owns it.
Mark State Private
@State is view-local, so mark it private. No one outside should reach in and set it directly.
@State private var isOn = falseBindings Stay Open
A @Binding is not private; the parent must be able to pass a value in when it creates the child.
The Dollar Sign Link
An owners @State turns into a child @Binding through the dollar sign. That is the bridge between the two.
ChildView(count: $count)Top of the Tree
The view highest up that truly owns the data uses @State. Everything below it borrows with @Binding.
A Simple Rule
Ask: do I create this value? If yes, @State. If a parent hands it to me, @Binding. That choice rarely fails you.
Wrong Choice, Real Bugs
Use @State where you meant @Binding and the child edits a copy. The parent never sees the change, and the UI quietly desyncs.
Same Value, Two Roles
One piece of data is @State in its owner and @Binding in every borrower. Same value, different responsibilities. 🔄
Quick Check
Quick check on choosing wrappers.
Recap: Own or Borrow
Use @State to own a value and @Binding to borrow one. Match the wrapper to who creates the data and your views stay in sync. 🧠
Często zadawane pytania
Czy lekcja „@State a @Binding” jest bezpłatna?
Tak — pełny tekst „@State a @Binding” 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 a @Binding”?
Wybierz właściwy sposób zarządzania każdą właściwością Ć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 4 z 4.
Ile czasu zajmuje lekcja „@State a @Binding”?
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 elementy podrzędne potrzebują @Binding
- Przekazywanie Binding w dół hierarchii
- Tworzenie wielokrotnego użytku wiersza przełącznika
- @State a @Binding