@State versus @Binding
Escolha a propriedade correta para cada tipo de propriedade.
@State versus @Binding é uma aula grátis de SwiftUI Academy no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de SwiftUI Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de SwiftUI Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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. 🧠
Aprenda Swift com um tutor de IA — grátis
Escreva e execute código real no seu navegador, obtenha ajuda instantânea de um tutor de IA 24/7 e continue de onde parou na web ou no app.
- Cursos
- 30
- Aulas
- 120
Perguntas Frequentes
A aula “@State versus @Binding” é grátis?
Sim — o texto completo de “@State versus @Binding” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de SwiftUI Academy, atualize para CoddyKit PRO. O curso de SwiftUI Academy inclui 4 aulas no total.
O que vou aprender em “@State versus @Binding”?
Escolha a propriedade correta para cada tipo de propriedade. Você pratica SwiftUI Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar SwiftUI Academy?
Nenhuma experiência prévia é necessária. SwiftUI Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “@State versus @Binding”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de SwiftUI Academy?
Sim. Cada aula de SwiftUI Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Por que as Filhas Precisam de @Binding
- Passando uma Binding para Baixo
- Criando uma Linha de Alternância Reutilizável
- @State versus @Binding