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 a view's state. It allows SwiftUI to monitor changes and automatically update the UI when the state changes.
Click to reveal answer
beginner
How does the @Published property wrapper work in Swift?
The @Published property wrapper marks a property so that changes to it will automatically notify any observers, typically used inside ObservableObject classes to update views or other listeners.
Click to reveal answer
beginner
In which type of Swift class is @Published commonly used?It is commonly used inside classes that conform to ObservableObject, which allows SwiftUI views to observe and react to changes in the data.
Click to reveal answer
beginner
What happens when a @State variable changes in a SwiftUI view?
When a @State variable changes, SwiftUI automatically re-renders the view to reflect the new state, keeping the UI in sync with the data.
Click to reveal answer
intermediate
Can @State be used outside of SwiftUI views?
No, @State is designed specifically for use inside SwiftUI views to manage local state. For shared or external state, other wrappers like @Published and ObservableObject are used.
Click to reveal answer
What does the @State property wrapper do in SwiftUI?
✗ Incorrect
The @State wrapper stores local state inside a SwiftUI view and triggers UI updates when the state changes.
Which property wrapper is used inside ObservableObject classes to notify changes?
✗ Incorrect
@Published marks properties inside ObservableObject classes to notify observers when the value changes.
What must a class conform to in order to use @Published effectively with SwiftUI?
✗ Incorrect
Classes must conform to ObservableObject to use @Published so SwiftUI views can observe changes.
Which of these is true about @State variables?
✗ Incorrect
@State variables are for local state inside a single SwiftUI view, not for sharing across views.
If you want a SwiftUI view to update when a model changes, which combination is correct?
✗ Incorrect
Use @Published in the model class conforming to ObservableObject and @ObservedObject in the view to observe changes.
Explain how @State and @Published differ in managing data and updating SwiftUI views.
Think about where the data lives and who needs to know about changes.
You got /4 concepts.
Describe a simple example scenario where you would use @State and another where you would use @Published.
Consider local UI state versus shared app data.
You got /3 concepts.