Challenge - 5 Problems
ObservedObject Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
How does @ObservedObject update the UI?
Consider a SwiftUI view using an @ObservedObject to watch a data model. What happens when a published property in the observed object changes?
iOS Swift
class Counter: ObservableObject { @Published var count = 0 } struct ContentView: View { @ObservedObject var counter = Counter() var body: some View { Text("Count: \(counter.count)") } }
Attempts:
2 left
💡 Hint
Think about how SwiftUI listens to changes in ObservableObject classes.
✗ Incorrect
When a property marked with @Published inside an ObservableObject changes, any SwiftUI view observing it with @ObservedObject automatically refreshes its body to reflect the new data.
❓ lifecycle
intermediate2:00remaining
When is an @ObservedObject recreated in SwiftUI?
Given a SwiftUI view with an @ObservedObject property initialized inside the view struct, when does the observed object get recreated?
iOS Swift
class TimerModel: ObservableObject { @Published var time = 0 } struct TimerView: View { @ObservedObject var timer = TimerModel() var body: some View { Text("Time: \(timer.time)") } }
Attempts:
2 left
💡 Hint
Think about how SwiftUI structs are recreated and what happens to properties initialized inside them.
✗ Incorrect
When you initialize an @ObservedObject inside a view struct, it gets recreated every time the view redraws, which can cause unexpected behavior. Usually, @ObservedObject expects the object to be created outside and passed in.
🔧 Debug
advanced2:00remaining
Why does the UI not update when using @ObservedObject?
You have this code but the UI does not update when the data changes. What is the most likely cause?
iOS Swift
class DataModel: ObservableObject { var value = 0 } struct ContentView: View { @ObservedObject var model = DataModel() var body: some View { Text("Value: \(model.value)") } }
Attempts:
2 left
💡 Hint
Check how SwiftUI knows when to update views based on ObservableObject changes.
✗ Incorrect
Only properties marked with @Published inside an ObservableObject send change notifications. Without @Published, SwiftUI does not know when the property changes and won't update the UI.
advanced
2:00remaining
Passing @ObservedObject between views
You want to share a data model between two SwiftUI views so both update when the model changes. Which approach correctly shares the @ObservedObject?
iOS Swift
class UserSettings: ObservableObject { @Published var username = "Guest" } struct ParentView: View { @StateObject var settings = UserSettings() var body: some View { ChildView(settings: settings) } } struct ChildView: View { @ObservedObject var settings: UserSettings var body: some View { Text("User: \(settings.username)") } }
Attempts:
2 left
💡 Hint
Think about which view owns the data and which views observe it.
✗ Incorrect
The parent view owns the data model using @StateObject. It passes the model down to child views as @ObservedObject so they observe changes without owning the data.
🧠 Conceptual
expert2:00remaining
Difference between @ObservedObject and @StateObject
Which statement best describes the difference between @ObservedObject and @StateObject in SwiftUI?
Attempts:
2 left
💡 Hint
Consider which property wrapper manages the lifecycle of the object.
✗ Incorrect
@StateObject is used to create and keep the ObservableObject alive for the life of the view. @ObservedObject is used to observe an object created elsewhere.