@State para Objetos de Propriedade
Mantenha a propriedade de um modelo observável com @State.
@State para Objetos de Propriedade é uma aula grátis de SwiftUI Academy no CoddyKit. Esta é a aula 3 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.
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. ✨
Perguntas Frequentes
A aula “@State para Objetos de Propriedade” é grátis?
Sim — o texto completo de “@State para Objetos de Propriedade” é 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 para Objetos de Propriedade”?
Mantenha a propriedade de um modelo observável com @State. 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 3 de 4.
Quanto tempo leva a aula “@State para Objetos de Propriedade”?
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
- Do Estado da Visualização para uma Classe de Modelo
- A Macro @Observable
- @State para Objetos de Propriedade
- Compartilhando Modelos via @Environment