0
0
iOS Swiftmobile~20 mins

@ObservedObject in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ObservedObject Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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)")
  }
}
AThe app crashes because @ObservedObject cannot be used with classes.
BThe Text view does not update unless the view is recreated manually.
CThe Text view updates automatically to show the new count value.
DThe Text view updates only if the count property is marked with @State.
Attempts:
2 left
💡 Hint
Think about how SwiftUI listens to changes in ObservableObject classes.
lifecycle
intermediate
2: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)")
  }
}
AThe TimerModel instance is created once and reused across redraws.
BEvery time the view redraws, a new TimerModel instance is created.
CThe TimerModel instance is never created because @ObservedObject requires external injection.
DThe TimerModel instance is created only when the app launches.
Attempts:
2 left
💡 Hint
Think about how SwiftUI structs are recreated and what happens to properties initialized inside them.
🔧 Debug
advanced
2: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)")
  }
}
AThe value property is not marked with @Published, so changes are not announced.
BThe DataModel class must inherit from NSObject for @ObservedObject to work.
CThe Text view must be inside a VStack to update properly.
DThe @ObservedObject property wrapper must be replaced with @State for updates.
Attempts:
2 left
💡 Hint
Check how SwiftUI knows when to update views based on ObservableObject changes.
navigation
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)")
  }
}
AParentView creates the UserSettings with @StateObject and passes it to ChildView as @ObservedObject.
BUserSettings should be passed as a plain variable without property wrappers.
CChildView should use @StateObject to create UserSettings instead of @ObservedObject.
DBoth ParentView and ChildView create their own UserSettings with @ObservedObject.
Attempts:
2 left
💡 Hint
Think about which view owns the data and which views observe it.
🧠 Conceptual
expert
2:00remaining
Difference between @ObservedObject and @StateObject
Which statement best describes the difference between @ObservedObject and @StateObject in SwiftUI?
A@StateObject is used only for structs; @ObservedObject is used only for classes.
B@ObservedObject creates and owns the ObservableObject instance; @StateObject only observes an existing instance.
CBoth @ObservedObject and @StateObject create new instances every time the view redraws.
D@StateObject creates and owns the ObservableObject instance; @ObservedObject only observes an existing instance.
Attempts:
2 left
💡 Hint
Consider which property wrapper manages the lifecycle of the object.