0
0
iOS Swiftmobile~5 mins

@State property wrapper in iOS Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
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
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
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
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
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
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.