Bird
0
0

You have a parent view that creates a Settings object and passes it to two child views. Which is the best way to share this data so both children update when settings change?

hard📝 Application Q15 of 15
iOS Swift - State Management in SwiftUI
You have a parent view that creates a Settings object and passes it to two child views. Which is the best way to share this data so both children update when settings change?
AUse <code>@EnvironmentObject</code> in children without passing from parent
BCreate separate <code>@ObservedObject</code> instances in each child view
CUse <code>@State</code> in the parent and <code>@Binding</code> in children for the settings
DDeclare <code>@ObservedObject</code> in both child views and pass the same <code>Settings</code> instance from the parent
Step-by-Step Solution
Solution:
  1. Step 1: Understand data sharing with @ObservedObject

    Passing the same instance to children with @ObservedObject keeps them in sync.
  2. Step 2: Evaluate other options

    Creating separate instances duplicates data; @State/@Binding is for simple values; @EnvironmentObject requires environment setup.
  3. Final Answer:

    Declare @ObservedObject in both child views and pass the same Settings instance from the parent -> Option D
  4. Quick Check:

    Share one @ObservedObject instance for synced updates [OK]
Quick Trick: Pass one @ObservedObject instance to children for shared updates [OK]
Common Mistakes:
  • Creating multiple instances causing unsynced data
  • Confusing @Binding with @ObservedObject for complex objects
  • Using @EnvironmentObject without setup

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes