State holds the current data of your app. When state changes, the app updates what you see automatically. This keeps the app fresh and interactive without extra work.
Why state drives reactive UI updates in iOS Swift
@State private var variableName: Type = initialValue
@State private var isOn: Bool = false
@State private var count: Int = 0@State private var username: String = ""This app shows a toggle switch and a text label. When you flip the switch, the text changes automatically because the @State variable isOn updates and triggers the UI to refresh.
import SwiftUI struct ContentView: View { @State private var isOn: Bool = false var body: some View { VStack(spacing: 20) { Text(isOn ? "Switch is ON" : "Switch is OFF") .font(.title) Toggle("Toggle Switch", isOn: $isOn) .padding() } .padding() } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
Always use @State for simple, local state inside a view.
Changing state variables causes SwiftUI to re-run the view's body and update the screen.
Do not change state variables directly from outside the view; use bindings or other state management tools.
State holds data that can change and affect the UI.
Mark variables with @State to make the UI update automatically when they change.
This makes your app interactive and keeps the screen in sync with your data.
Practice
@State in a SwiftUI view?Solution
Step 1: Understand the role of
@State@Statemarks a variable as reactive, so when it changes, the UI updates.Step 2: Compare other options
Permanent storage is done with other tools, creating views or handling gestures are unrelated to
@State.Final Answer:
To make the UI update automatically when the data changes -> Option BQuick Check:
State drives UI updates = C [OK]
- Thinking @State saves data permanently
- Confusing @State with view creation
- Assuming @State handles gestures
Solution
Step 1: Recall SwiftUI state syntax
The correct syntax uses@Statebefore the variable declaration.Step 2: Check each option
@State var count: Int = 0 uses@State var count: Int = 0, which is correct. Others have wrong order or keywords.Final Answer:
@State var count: Int = 0 -> Option DQuick Check:
Correct @State syntax = D [OK]
- Placing @State after var
- Using let instead of var with @State
- Using unknown keywords like 'state'
struct CounterView: View {
@State var count = 0
var body: some View {
VStack {
Text("Count: \(count)")
Button("Increment") {
count += 1
}
}
}
}
What happens when the button is tapped?Solution
Step 1: Understand @State effect on UI
The@Statevariablecountchanges when the button is tapped, triggering UI update.Step 2: Analyze the button action
The button incrementscountby 1, so the Text showing count updates automatically.Final Answer:
The count variable increases and the Text updates to show the new count -> Option AQuick Check:
@State changes update UI = A [OK]
- Thinking UI won't update without manual refresh
- Assuming count is immutable
- Ignoring the button's action closure
struct ExampleView: View {
@State var text: String
var body: some View {
Text(text)
}
}Solution
Step 1: Check @State variable declaration
@Statevariables must have an initial value because SwiftUI manages their storage.Step 2: Verify other parts
Text can display String, body is a computed property, and struct inherits from View, not UIViewController.Final Answer:
The @State variable must have an initial value -> Option AQuick Check:
@State needs initial value = B [OK]
- Leaving @State variables uninitialized
- Confusing View with UIViewController
- Misunderstanding Text view usage
Solution
Step 1: Identify correct @State usage and UI update
struct ToggleView: View { @State var isOn = false var body: some View { Button(isOn ? "ON" : "OFF") { isOn.toggle() } } } uses@StateforisOnand updates it inside Button action, changing the button label reactively.Step 2: Check other options for issues
struct ToggleView: View { var isOn = false var body: some View { Button(isOn ? "ON" : "OFF") { isOn = !isOn } } } lacks @State, so UI won't update. struct ToggleView: View { @State var isOn = false var body: some View { Text(isOn ? "ON" : "OFF") } } shows text but no button to toggle. struct ToggleView: View { @State var isOn = false var body: some View { Button("Toggle") { isOn = !isOn } } } toggles state but button label is static.Final Answer:
struct ToggleView: View { @State var isOn = false var body: some View { Button(isOn ? "ON" : "OFF") { isOn.toggle() } } } -> Option CQuick Check:
@State + toggle + reactive label = A [OK]
- Forgetting @State causes no UI update
- Using static button labels
- Not toggling state inside button action
Ready to prove it?
Pass the test and this pattern moves unknown
