Pick the fix
An iOS 17 app creates a SettingsModel (@Observable) at the root, and many deep views read it. Which setup?
Hint 1 of 2
Who owns the model?
Hint 2 of 2
Which wrappers belong to Observation?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Root: @State var settings = SettingsModel() plus .environment(settings); readers: @Environment(SettingsModel.self) var settings
Correct. The root owns it, and the environment shares it without passing it through every view.
Why not the others?
- B Root:
@StateObjectplus.environmentObject(settings). Those are for ObservableObject. An @Observable class doesn't conform, so this doesn't compile. - C Pass it through every initializer. It works, but prop drilling through ten layers is noisy and fragile.
- D
static let shared = SettingsModel(). A singleton hides the dependency and makes previews and tests harder.