@State frente a @Binding
Elija la propiedad adecuada para cada caso de ownership.
@State frente a @Binding es una lección gratuita de SwiftUI Academy en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de SwiftUI Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de SwiftUI Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en 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. 🧠
Aprende Swift con un tutor de IA — gratis
Escribe y ejecuta código real en tu navegador, obtén ayuda instantánea de un tutor de IA disponible 24/7 y continúa donde lo dejaste en la web o en la aplicación.
- Cursos
- 30
- Lecciones
- 120
Preguntas frecuentes
¿La lección «@State frente a @Binding» es gratis?
Sí — el texto completo de «@State frente a @Binding» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de SwiftUI Academy, actualiza a CoddyKit PRO. El curso de SwiftUI Academy incluye 4 lecciones en total.
¿Qué aprenderé en «@State frente a @Binding»?
Elija la propiedad adecuada para cada caso de ownership. Practicas SwiftUI Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar SwiftUI Academy?
No se requiere experiencia previa. SwiftUI Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «@State frente a @Binding»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de SwiftUI Academy?
Sí. Cada lección de SwiftUI Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Por qué los elementos secundarios necesitan @Binding
- Pasar un Binding hacia abajo
- Crear una fila Toggle reutilizable
- @State frente a @Binding