0
0
iOS Swiftmobile~20 mins

ObservableObject protocol in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ObservableObject Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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?
AThe view ignores the change until manually refreshed.
BThe ObservableObject stops publishing changes.
CThe app crashes due to a runtime error.
DThe view automatically updates to reflect the new property value.
Attempts:
2 left
💡 Hint
Think about how SwiftUI reacts to changes in observed data.
lifecycle
intermediate
2:00remaining
When is the objectWillChange publisher triggered in ObservableObject?
In an ObservableObject, which action triggers the objectWillChange publisher to notify views?
AWhen any @Published property is about to change.
BOnly when the ObservableObject is created.
CWhen the ObservableObject is deallocated.
DWhen a method is called without changing properties.
Attempts:
2 left
💡 Hint
Focus on what causes SwiftUI to know data is changing.
📝 Syntax
advanced
2: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
}
A
class MyModel {
  @Published var count: Int = 0
}
B
class MyModel: ObservableObject {
  @Published var count: Int = 0
}
C
struct MyModel: ObservableObject {
  @Published var count: Int = 0
}
D
class MyModel: ObservableObject {
  var count: Int = 0
}
Attempts:
2 left
💡 Hint
Remember ObservableObject must be a class and @Published marks properties to notify changes.
🔧 Debug
advanced
2: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)") } }
AThe Text view cannot display variables from ObservableObject.
BThe @StateObject property wrapper is used incorrectly.
CThe count property is not marked with @Published, so changes are not published.
DThe Counter class does not conform to ObservableObject.
Attempts:
2 left
💡 Hint
Check how properties notify changes to SwiftUI.
🧠 Conceptual
expert
2:00remaining
What is the role of objectWillChange in ObservableObject?
In the ObservableObject protocol, what is the purpose of the objectWillChange publisher?
AIt signals subscribers before any @Published property changes, allowing views to update.
BIt stores the current state of all @Published properties.
CIt triggers only after all properties have changed.
DIt prevents views from updating when properties change.
Attempts:
2 left
💡 Hint
Think about timing of notifications to views.