Declaring @State Properties
Add @State to hold mutable view-local data.
Declaring @State Properties is a free SwiftUI Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. ✅
Frequently asked questions
Is the “Declaring @State Properties” lesson free?
Yes — the full text of “Declaring @State Properties” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “Declaring @State Properties”?
Add @State to hold mutable view-local data. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Declaring @State Properties” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why Views Need State
- Declaring @State Properties
- Building a Tap Counter
- Toggling Visibility with State