0
0
Swiftprogramming~5 mins

Built-in property wrappers (@State, @Published) in 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 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?
ADefines a constant value
BPublishes changes to external observers
CStores local state and triggers UI updates when changed
DCreates a new SwiftUI view
Which property wrapper is used inside ObservableObject classes to notify changes?
A@State
B@EnvironmentObject
C@Binding
D@Published
What must a class conform to in order to use @Published effectively with SwiftUI?
AObservableObject
BCodable
CIdentifiable
DView
Which of these is true about @State variables?
AThey are used for local state inside a single view
BThey can be shared across multiple views directly
CThey automatically persist data to disk
DThey are used to create bindings to environment values
If you want a SwiftUI view to update when a model changes, which combination is correct?
AUse @State in the model
BUse @Published in an ObservableObject model and @ObservedObject in the view
CUse @Binding in the model
DUse @EnvironmentObject in the model only
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.