The @State property wrapper lets your app remember and update values that can change over time. It helps your app update the screen automatically when data changes.
@State property wrapper in iOS Swift
@State private var variableName: Type = initialValue
@State only inside SwiftUI views to hold simple, local data.private to keep it safe inside the view.@State private var isOn: Bool = false
@State private var count: Int = 0@State private var username: String = ""This app shows a button and a text label. Each time you tap the button, the number increases and the text updates automatically. This happens because count is marked with @State, so SwiftUI knows to refresh the screen when it changes.
import SwiftUI struct ContentView: View { @State private var count: Int = 0 var body: some View { VStack(spacing: 20) { Text("Button tapped: \(count) times") .font(.title) Button("Tap me") { count += 1 } .padding() .background(Color.blue) .foregroundColor(.white) .cornerRadius(8) } .padding() } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
Only use @State for simple, local data inside one view.
For sharing data between views, use other tools like @ObservedObject or @EnvironmentObject.
Changing a @State variable automatically refreshes the view to show the new data.
@State keeps track of changing data inside a SwiftUI view.
It automatically updates the screen when the data changes.
Use it for simple, local variables like counters, toggles, or text inputs.
Practice
@State property wrapper in SwiftUI?Solution
Step 1: Understand @State role
@State is used to hold data that can change inside a SwiftUI view.Step 2: Connect data change to UI update
When @State data changes, SwiftUI automatically refreshes the view to show the new data.Final Answer:
To keep track of data that can change and update the UI automatically -> Option AQuick Check:
@State updates UI on data change = B [OK]
- Thinking @State stores permanent data
- Confusing @State with constants
- Using @State for functions
@State variable for an integer counter in SwiftUI?Solution
Step 1: Check correct @State syntax
The correct syntax places @State before the variable declaration and usually uses private for encapsulation.Step 2: Identify valid declaration
@State private var counter = 0 uses '@State private var counter = 0' which is the recommended and correct form.Final Answer:
@State private var counter = 0 -> Option DQuick Check:
@State before var, private optional = D [OK]
- Placing @State after var
- Missing var keyword
- Using let instead of var
struct ContentView: View {
@State private var count = 0
var body: some View {
VStack {
Text("Count: \(count)")
Button("Add") {
count += 1
}
}
}
}
What happens when the "Add" button is tapped?Solution
Step 1: Understand @State behavior on change
When the button increments count, the @State variable changes.Step 2: UI updates automatically
SwiftUI detects the change and refreshes the Text to show the new count value.Final Answer:
The count increases by 1 and the Text updates to show the new count -> Option CQuick Check:
@State change triggers UI update = C [OK]
- Thinking private stops UI updates
- Assuming UI won't refresh on state change
- Believing @State variables are immutable
struct ContentView: View {
@State var text: String
var body: some View {
TextField("Enter text", text: $text)
}
}Solution
Step 1: Check @State variable declaration
@State variables must be initialized with a value when declared.Step 2: Identify missing initial value
Here, 'text' has no initial value, causing a compile error.Final Answer:
@State variables must have an initial value -> Option BQuick Check:
@State needs initial value = A [OK]
- Forgetting to initialize @State variables
- Thinking @State can't bind to TextField
- Confusing struct rules with @State usage
Solution
Step 1: Check @State declaration and initialization
struct ContentView: View { @State private var isOn = false var body: some View { VStack { Toggle("Switch", isOn: $isOn) Text(isOn ? "On" : "Off") } } } correctly declares @State private var isOn = false, initializing the state.Step 2: Verify Toggle binding
The Toggle uses $isOn to bind to the state variable, enabling UI updates on toggle.Step 3: Confirm Text updates with state
The Text view uses a ternary to show "On" or "Off" based on isOn value.Final Answer:
code correctly implements the toggle and text update -> Option AQuick Check:
@State with $ binding updates UI = A [OK]
- Not initializing @State variable
- Missing $ for binding in Toggle
- Declaring @State without initial value
Ready to prove it?
Pass the test and this pattern moves unknown
