Bird
0
0

Identify the error in this ObservableObject class that prevents UI updates:

medium📝 Debug Q14 of 15
iOS Swift - State Management in SwiftUI
Identify the error in this ObservableObject class that prevents UI updates:
class TimerData: ObservableObject {
  var seconds = 0

  func tick() {
    seconds += 1
  }
}
AMissing @Published on seconds property
BClass must inherit from NSObject
Ctick() must be marked as @Published
DObservableObject requires struct, not class
Step-by-Step Solution
Solution:
  1. Step 1: Check property change notification

    The seconds property is not marked with @Published, so changes won't notify views.
  2. Step 2: Verify other options

    ObservableObject does not require NSObject inheritance, methods can't be @Published, and it must be a class.
  3. Final Answer:

    Missing @Published on seconds property -> Option A
  4. Quick Check:

    @Published missing = no UI update [OK]
Quick Trick: Properties must be @Published to notify UI [OK]
Common Mistakes:
  • Thinking methods can be @Published
  • Believing ObservableObject needs NSObject
  • Confusing class and struct requirements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes