Bird
0
0

Which of the following is the correct way to declare an observed object in a SwiftUI view?

easy📝 Syntax Q12 of 15
iOS Swift - State Management in SwiftUI
Which of the following is the correct way to declare an observed object in a SwiftUI view?
A@ObservedObject var model: MyModel
B@ObservedObject var model: MyModel = MyModel()
C@ObservedObject model = MyModel()
D@ObservedObject var model: MyModel?
Step-by-Step Solution
Solution:
  1. Step 1: Understand @ObservedObject declaration

    @ObservedObject properties are typically declared without initialization in the view, expecting the instance to be injected.
  2. Step 2: Check syntax correctness

    @ObservedObject var model: MyModel is the correct declaration; initializing it inside the view is not recommended.
  3. Final Answer:

    @ObservedObject var model: MyModel -> Option A
  4. Quick Check:

    Declare @ObservedObject without initialization for injection [OK]
Quick Trick: Declare @ObservedObject properties without initializing inside the view [OK]
Common Mistakes:
  • Initializing @ObservedObject inside the view
  • Omitting the var keyword
  • Using optional type without proper handling

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes