Bird
0
0

Identify the error in this SwiftUI view using @ObservedObject:

medium📝 Debug Q14 of 15
iOS Swift - State Management in SwiftUI
Identify the error in this SwiftUI view using @ObservedObject:
class TimerModel: ObservableObject {
  @Published var time = 0
}

struct TimerView: View {
  @ObservedObject var timer: TimerModel
  var body: some View {
    Text("Time: \(timer.time)")
  }
}
AThe TimerModel class is missing ObservableObject conformance
BThe @ObservedObject property is not initialized
CThe @Published property should be private
DThe Text view syntax is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Check class conformance

    TimerModel correctly conforms to ObservableObject.
  2. Step 2: Check @ObservedObject initialization

    The timer property is declared but not initialized, causing a compile error.
  3. Final Answer:

    The @ObservedObject property is not initialized -> Option B
  4. Quick Check:

    @ObservedObject must be initialized or injected [OK]
Quick Trick: Always initialize or inject @ObservedObject properties [OK]
Common Mistakes:
  • Forgetting to initialize @ObservedObject
  • Assuming @Published must be private
  • Misreading Text string interpolation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes