Bird
0
0

What will happen if you forget to inject .modelContainer into the SwiftUI app's environment?

medium📝 Predict Output Q5 of 15
iOS Swift - Local Data Persistence
What will happen if you forget to inject .modelContainer into the SwiftUI app's environment?
AViews using @Environment(\.modelContext) will crash at runtime.
BThe app will compile but data won't save.
CThe app will ignore the model and show empty UI.
DThe app will automatically create a default container.
Step-by-Step Solution
Solution:
  1. Step 1: Understand environment injection for SwiftData

    SwiftUI views need the model container injected via .modelContainer modifier to access data context.
  2. Step 2: Consequence of missing injection

    If missing, accessing @Environment(\.modelContext) causes a runtime crash because context is nil.
  3. Final Answer:

    Views using @Environment(\.modelContext) will crash at runtime. -> Option A
  4. Quick Check:

    Missing modelContainer injection = runtime crash [OK]
Quick Trick: Always inject .modelContainer in app environment [OK]
Common Mistakes:
  • Assuming app auto-creates container
  • Thinking app compiles but silently fails
  • Expecting empty UI instead of crash

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes