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
✗ Incorrect
The ObservableObject protocol allows a class to notify SwiftUI views when its @Published properties change.
What SwiftUI property wrapper is commonly used to observe an ObservableObject in a view?
A@EnvironmentObject
B@StateObject
C@Published
D@Binding
✗ Incorrect
@StateObject creates and owns an observable object in a view, updating the UI when @Published properties change.
What happens if you change an @Published property value?
AThe app crashes
BNothing happens
CThe UI updates automatically
DYou must manually refresh the UI
✗ Incorrect
Changing an @Published property triggers automatic UI updates in SwiftUI views observing it.
Can @Published be used with structs?
AOnly with protocols
BYes, with any data type
COnly with enums
DNo, only with classes conforming to ObservableObject
✗ Incorrect
@Published requires a class conforming to ObservableObject to send change notifications.
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
✗ Incorrect
@Published helps keep the UI in sync with data by automatically notifying views to update.
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.
Practice
(1/5)
1. What is the main purpose of marking a property with @Published in SwiftUI?
easy
A. To make the property constant
B. To make the property private
C. To automatically notify views when the property changes
D. To prevent the property from being saved
Solution
Step 1: Understand @Published role
@Published marks a property to send updates when its value changes.
Step 2: Connect to UI updates
This automatic notification helps SwiftUI views update when data changes.
Final Answer:
To automatically notify views when the property changes -> Option C
Quick Check:
@Published = automatic update notification [OK]
Hint: Remember: @Published means auto-update UI on change [OK]
Common Mistakes:
Thinking @Published makes property private
Confusing @Published with constant declaration
Assuming @Published saves data permanently
2. Which of the following is the correct way to declare a @Published property inside an ObservableObject class?
easy
A. struct Model: ObservableObject { @Published var count: Int = 0 }
B. class Model: ObservableObject { @Published var count: Int = 0 }
C. class Model { @Published var count: Int = 0 }
D. class Model: ObservableObject { var count: @Published Int = 0 }
Solution
Step 1: Check class and ObservableObject conformance
@Published must be inside a class conforming to ObservableObject, so class Model: ObservableObject is correct.
Step 2: Verify property declaration syntax
The correct syntax is @Published var count: Int = 0, not var count: @Published Int.
Final Answer:
class Model: ObservableObject { @Published var count: Int = 0 } -> Option B
Quick Check:
Correct syntax = class Model: ObservableObject { @Published var count: Int = 0 } [OK]
Hint: Remember: @Published goes before var inside ObservableObject class [OK]
Common Mistakes:
Using struct instead of class for ObservableObject
Placing @Published after the type
Not conforming class to ObservableObject
3. Given the code below, what will be printed when model.count = 5 is executed?
class Model: ObservableObject {
@Published var count: Int = 0
}
let model = Model()
model.$count.sink { print("Count changed to \($0)") }
medium
A. Count changed to 5
B. Count changed to 0
C. No output
D. Compile error
Solution
Step 1: Understand @Published publisher
@Published creates a publisher accessible via $property, here $count, which emits new values on change.
Step 2: Analyze sink subscription and assignment
The sink closure prints the new value when count changes. Setting model.count = 5 triggers the print with 5.
Final Answer:
Count changed to 5 -> Option A
Quick Check:
@Published publishes new values = Count changed to 5 [OK]
Hint: Remember: $property publishes changes, sink prints new value [OK]
Common Mistakes:
Expecting output before assignment
Confusing $count with count
Thinking no output without explicit call
4. Identify the error in this code snippet:
class Model: ObservableObject {
@Published var name: String
init() {
name = ""
}
}
let model = Model()
model.name = "Alice"
medium
A. Missing initial value for @Published property
B. ObservableObject conformance is missing
C. Cannot assign to @Published property outside class
D. No error, code works fine
Solution
Step 1: Check @Published property initialization
The property name is initialized in init(), which is valid for non-optional properties.
Step 2: Verify ObservableObject conformance and usage
Class conforms to ObservableObject and property is assigned outside class, which is allowed.
Final Answer:
No error, code works fine -> Option A
Quick Check:
Proper init and conformance = No error, code works fine [OK]
Hint: Remember: @Published properties can be initialized in init() [OK]
Common Mistakes:
Assuming @Published needs default value inline
Thinking ObservableObject conformance is missing
Believing external assignment is disallowed
5. You want to create a SwiftUI app where a @Published property updates a label text automatically. Which of the following is the best way to connect the @Published property to the UI?
hard
A. Use @EnvironmentObject without passing the object
B. Use @State in the view for the property
C. Manually call setNeedsDisplay() after changing the property
D. Use @ObservedObject in the view to watch the ObservableObject
Solution
Step 1: Understand property wrappers for UI updates
@Published properties notify changes, but the view must observe the object using @ObservedObject to update UI automatically.
Step 2: Evaluate other options
@State is for local view state, not external objects; setNeedsDisplay() is UIKit, not SwiftUI; @EnvironmentObject requires injection.
Final Answer:
Use @ObservedObject in the view to watch the ObservableObject -> Option D
Quick Check:
@Published + @ObservedObject = auto UI update [OK]
Hint: Connect @Published with @ObservedObject in views [OK]
Common Mistakes:
Using @State instead of @ObservedObject for external data
Trying manual UI refresh calls in SwiftUI
Using @EnvironmentObject without proper injection
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.