Bird
0
0

Which steps must you follow to correctly implement this shared state?

hard📝 Application Q15 of 15
iOS Swift - State Management in SwiftUI
You want to share a user profile object across multiple views using @EnvironmentObject. Which steps must you follow to correctly implement this shared state? A) Declare the profile class as ObservableObject with @Published properties. B) Create an instance of the profile and inject it using .environmentObject() at the app root. C) Use @EnvironmentObject in all views that need access to the profile. D) Pass the profile instance manually to each view initializer instead of using @EnvironmentObject.
AOnly A, B, and C are needed.
BOnly B, C, and D are needed.
COnly A and B are needed.
DAll A, B, C, and D are needed.
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct steps for shared state

    The profile class must conform to ObservableObject with @Published properties (A). Then create and inject it at the app root with .environmentObject() (B). Views needing access use @EnvironmentObject (C).
  2. Step 2: Understand why D is incorrect

    Passing the profile manually (D) defeats the purpose of @EnvironmentObject, which avoids manual passing.
  3. Final Answer:

    Only A, B, and C are needed. -> Option A
  4. Quick Check:

    ObservableObject + inject + @EnvironmentObject = C [OK]
Quick Trick: Inject once, declare ObservableObject, use @EnvironmentObject in views [OK]
Common Mistakes:
  • Passing environment object manually defeats its purpose
  • Forgetting to mark class ObservableObject
  • Not injecting at app root

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes