0
0
iOS Swiftmobile~5 mins

@Published properties in iOS Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the @Published property wrapper do in Swift?
It marks a property so that changes to it will automatically notify any observers, typically used in SwiftUI to update the UI when data changes.
Click to reveal answer
intermediate
How do you observe changes to an @Published property in SwiftUI?
By using an <code>ObservableObject</code> class with <code>@Published</code> properties and observing it in a SwiftUI view with <code>@StateObject</code> or <code>@ObservedObject</code>.
Click to reveal answer
beginner
Can @Published be used outside of classes conforming to ObservableObject?
No, @Published works only inside classes that conform to ObservableObject because it needs to send change notifications.
Click to reveal answer
beginner
What happens in the UI when an @Published property changes?
The SwiftUI view observing the property automatically refreshes to reflect the new data without manual updates.
Click to reveal answer
intermediate
Why is @Published useful for building reactive apps?
Because it simplifies keeping the UI in sync with data changes by automatically notifying views to update when data changes.
Click to reveal answer
Which protocol must a class conform to for @Published properties to work properly?
AObservableObject
BCodable
CIdentifiable
DEquatable
What SwiftUI property wrapper is commonly used to observe an ObservableObject in a view?
A@EnvironmentObject
B@StateObject
C@Published
D@Binding
What happens if you change an @Published property value?
AThe app crashes
BNothing happens
CThe UI updates automatically
DYou must manually refresh the UI
Can @Published be used with structs?
AOnly with protocols
BYes, with any data type
COnly with enums
DNo, only with classes conforming to ObservableObject
Which of these is a benefit of using @Published in SwiftUI?
AAutomatic UI updates when data changes
BFaster app launch times
CBetter memory management
DSimpler networking code
Explain how @Published properties work with ObservableObject to update SwiftUI views.
Think about how data changes notify the UI.
You got /4 concepts.
    Describe a simple example of using @Published in a SwiftUI app to update a text label when data changes.
    Imagine a counter that updates on button tap.
    You got /4 concepts.