What is the purpose of the @State property wrapper in SwiftUI?
The @State property wrapper is used to declare a source of truth for data that can change over time within a SwiftUI view. It allows the view to update its UI automatically when the data changes.
Click to reveal answer
beginner
How does @State help with UI updates in SwiftUI?
When a @State variable changes, SwiftUI automatically re-renders the parts of the view that depend on that variable, keeping the UI in sync with the data.
Click to reveal answer
intermediate
Can you explain why @State variables must be private in SwiftUI views?
Because @State variables are the source of truth owned by the view, they should be private to prevent external modification. This keeps data flow predictable and avoids bugs.
Click to reveal answer
beginner
What happens if you try to modify a regular variable in a SwiftUI view instead of a @State variable?
Modifying a regular variable does not trigger a UI update. The view will not refresh to reflect the change, so the UI and data can get out of sync.
Click to reveal answer
beginner
Show a simple example of declaring a @State variable in a SwiftUI view.
Example:
struct ContentView: View {
@State private var count = 0
var body: some View {
Button("Tap count: \(count)") {
count += 1
}
}
}
Click to reveal answer
What does the @State property wrapper do in SwiftUI?
AManages network requests automatically
BDefines a constant value that never changes
CStores data that triggers UI updates when changed
DCreates a global variable accessible anywhere
✗ Incorrect
@State stores data that when changed, causes the SwiftUI view to update its UI automatically.
Why should @State variables usually be private?
ATo prevent external code from modifying the view's source of truth
BBecause SwiftUI requires all variables to be private
CTo make the variable accessible globally
DTo improve app performance
✗ Incorrect
Keeping @State variables private ensures the view controls its own data, avoiding unexpected changes from outside.
What happens if you change a regular variable inside a SwiftUI view without @State?
AThe UI updates automatically
BThe UI will not update to reflect the change
CThe app crashes
DThe variable becomes a constant
✗ Incorrect
Only changes to @State variables trigger UI updates. Regular variables do not cause the view to refresh.
Which of these is a correct way to declare a @State variable?
A@State private var isOn = false
Bvar isOn = @State false
Cprivate var @State isOn = false
Dlet @State isOn = false
✗ Incorrect
The correct syntax is @State private var variableName = value.
When a @State variable changes, what does SwiftUI do?
ASaves the variable to disk
BDeletes the variable
CDoes nothing automatically
DRe-renders the view parts that depend on that variable
✗ Incorrect
SwiftUI automatically updates the UI to reflect changes in @State variables.
Explain in your own words what the @State property wrapper does in SwiftUI and why it is important.
Think about how changing data updates what the user sees.
You got /4 concepts.
Describe a simple example where you would use a @State variable in a SwiftUI app.
Imagine a button that changes a number on screen when tapped.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the @State property wrapper in SwiftUI?
easy
A. To keep track of data that can change and update the UI automatically
B. To create a constant value that never changes
C. To store data permanently on the device
D. To define a function inside a view
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 A
Quick Check:
@State updates UI on data change = B [OK]
Hint: Remember: @State means data changes update the screen [OK]
Common Mistakes:
Thinking @State stores permanent data
Confusing @State with constants
Using @State for functions
2. Which of the following is the correct way to declare a @State variable for an integer counter in SwiftUI?
easy
A. @State let counter: Int = 0
B. var @State counter: Int = 0
C. private var @State counter = 0
D. @State private var counter = 0
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 D
Quick Check:
@State before var, private optional = D [OK]
Hint: Always put @State before 'var' and use private if needed [OK]
Common Mistakes:
Placing @State after var
Missing var keyword
Using let instead of var
3. Consider this SwiftUI code snippet:
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?
medium
A. The app crashes because count is private
B. The count increases but the Text does not update
C. The count increases by 1 and the Text updates to show the new count
D. Nothing happens because @State variables cannot change
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 C
Quick Check:
@State change triggers UI update = C [OK]
Hint: @State changes always refresh the UI automatically [OK]
Common Mistakes:
Thinking private stops UI updates
Assuming UI won't refresh on state change
Believing @State variables are immutable
4. What is wrong with this SwiftUI code?
struct ContentView: View {
@State var text: String
var body: some View {
TextField("Enter text", text: $text)
}
}
medium
A. TextField cannot bind to @State variables
B. @State variables must have an initial value
C. The body property must return a Button, not TextField
D. @State cannot be used inside a struct
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 B
Quick Check:
@State needs initial value = A [OK]
Hint: Always give @State variables a starting value [OK]
Common Mistakes:
Forgetting to initialize @State variables
Thinking @State can't bind to TextField
Confusing struct rules with @State usage
5. You want to create a toggle switch that changes a text label between "On" and "Off" using @State. Which code snippet correctly implements this behavior?
hard
A. struct ContentView: View {
@State private var isOn = false
var body: some View {
VStack {
Toggle("Switch", isOn: $isOn)
Text(isOn ? "On" : "Off")
}
}
}
B. struct ContentView: View {
var isOn = false
var body: some View {
VStack {
Toggle("Switch", isOn: $isOn)
Text(isOn ? "On" : "Off")
}
}
}
C. struct ContentView: View {
@State var isOn: Bool
var body: some View {
VStack {
Toggle("Switch", isOn: $isOn)
Text(isOn ? "On" : "Off")
}
}
}
D. struct ContentView: View {
@State private var isOn = false
var body: some View {
VStack {
Toggle("Switch", isOn: isOn)
Text(isOn ? "On" : "Off")
}
}
}
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 A
Quick Check:
@State with $ binding updates UI = A [OK]
Hint: Use @State with $ for binding Toggle to update UI [OK]
Common Mistakes:
Not initializing @State variable
Missing $ for binding in Toggle
Declaring @State without initial value
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.