Challenge - 5 Problems
ObservableObject Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when an @Published property changes in an ObservableObject?
Consider a SwiftUI view observing an ObservableObject with an @Published property. What is the expected behavior when this property changes?
Attempts:
2 left
💡 Hint
Think about how SwiftUI reacts to changes in observed data.
✗ Incorrect
When an @Published property in an ObservableObject changes, SwiftUI automatically refreshes any views observing that object to show the updated data.
❓ lifecycle
intermediate2:00remaining
When is the objectWillChange publisher triggered in ObservableObject?
In an ObservableObject, which action triggers the objectWillChange publisher to notify views?
Attempts:
2 left
💡 Hint
Focus on what causes SwiftUI to know data is changing.
✗ Incorrect
The objectWillChange publisher emits before any @Published property changes, signaling views to update.
📝 Syntax
advanced2:00remaining
Identify the correct ObservableObject declaration
Which code snippet correctly declares an ObservableObject with a published property in Swift?
iOS Swift
class MyModel: ObservableObject { @Published var count: Int = 0 }
Attempts:
2 left
💡 Hint
Remember ObservableObject must be a class and @Published marks properties to notify changes.
✗ Incorrect
Only a class conforming to ObservableObject with @Published properties correctly notifies SwiftUI views.
🔧 Debug
advanced2:00remaining
Why does the SwiftUI view not update when ObservableObject changes?
Given this code, why does the SwiftUI view not update when the count changes?
class Counter: ObservableObject {
var count = 0
}
struct ContentView: View {
@StateObject var counter = Counter()
var body: some View {
Text("Count: \(counter.count)")
}
}
Attempts:
2 left
💡 Hint
Check how properties notify changes to SwiftUI.
✗ Incorrect
Without @Published, changes to count do not notify SwiftUI to update the view.
🧠 Conceptual
expert2:00remaining
What is the role of objectWillChange in ObservableObject?
In the ObservableObject protocol, what is the purpose of the objectWillChange publisher?
Attempts:
2 left
💡 Hint
Think about timing of notifications to views.
✗ Incorrect
objectWillChange emits before changes happen, so views can prepare to update smoothly.