Recall & Review
beginner
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:<br>
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?✗ Incorrect
@State stores data that when changed, causes the SwiftUI view to update its UI automatically.
Why should
@State variables usually be private?✗ 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?✗ 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?✗ Incorrect
The correct syntax is @State private var variableName = value.
When a
@State variable changes, what does SwiftUI do?✗ 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.