environmentObject(_:)
Supplies an ObservableObject to a view subhierarchy.
Declaration
nonisolated func environmentObject<T>(_ object: T) -> some Scene where T : ObservableObject
Parameters
- object:
The object to store and make available to the scene’s subhierarchy.
Discussion
The object can be read by any child by using EnvironmentObject:
final class Profile: ObservableObject { ... }
@main
struct MyApp: App {
var body: some View {
WindowGroup {
ContentView()
}
.environment(ProfileService.currentProfile)
}
}You then read the object inside ContentView or one of its descendants using the EnvironmentObject property wrapper:
struct ContentView: View {
@EnvironmentObject private var currentAccount: Account
var body: some View { ... }
}