> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/swiftui/view/environment/environmentobject.md).

# @EnvironmentObject

declare an environment object

[SwiftUI](/ios/swiftui.md) ⟩ [Data Flow](/ios/swiftui/data-flow.md) ⟩ @EnvironmentObject

{% hint style="success" %}
The <mark style="color:purple;">`@EnvironmentObject`</mark> [property wrapper](/ios/swift/type/prop/wrapper.md) is used to <mark style="color:yellow;">declare</mark> (in a [subview](/ios/swiftui/view/hierarchy/child.md)) an [environment object](broken://pages/QCGBkO6BC8iSChrBuMic) property <mark style="color:yellow;">provided by an</mark> <mark style="color:orange;">ancester view</mark>.&#x20;
{% endhint %}

```swift
// app
struct MyApp: App {
    
    // ⭐️ 1. initialize a state object (as the app's model data)
    @StateObject private var modelData = ModelData() 
    
    var body: some Scene {
        WindowGroup {
            RootView()
                // ⭐️ 2. put the model object in the environment.
                .environmentObject(modelData)         
        }
    }
}

// assume Subview is a subview of RootView in the view hierarchy
struct Subview: View {

    // ⭐️ 3. get the model object from environment.
    @EnvironmentObject var modelData: ModelData      
    
    var body: some View { ... }
}
```

{% hint style="danger" %}
Be sure to set a <mark style="color:yellow;">corresponding</mark> [model](/ios/master/term/data-model.md) <mark style="color:yellow;">instance</mark> on an <mark style="color:orange;">ancester view</mark> by calling its [.environmentObject(\_:)](https://developer.apple.com/documentation/swiftui/scene/environmentobject\(_:\)) modifier.
{% endhint %}

{% tabs %}
{% tab title="👥 相關" %}

* Use [ObservableObject](/ios/swiftui/view/state/observable-object/observableobject.md) to <mark style="color:yellow;">declare</mark> an <mark style="color:yellow;">observable object</mark> [type](/ios/swift/type.md).
* Use [@Published](/ios/swiftui/view/state/observable-object/published.md) to <mark style="color:yellow;">declare</mark> [published values](/ios/swiftui/view/state/observable-object/published-value.md) in an [observable object](/ios/swiftui/view/state/observable-object/observableobject.md) type.
* Use [@StateObject](/ios/swiftui/view/state/object/stateobject.md) to <mark style="color:yellow;">initialize</mark> an <mark style="color:yellow;">observable object</mark>.
* Use [.environmentObject(\_:)](https://developer.apple.com/documentation/swiftui/scene/environmentobject\(_:\)) to put the [model object](/ios/master/term/data-model.md) in the environment.
* Use <mark style="color:purple;">`@EnvironmentObject`</mark> to <mark style="color:yellow;">declare</mark> an [observable object](/ios/swiftui/view/state/observable-object/observableobject.md) <mark style="color:yellow;">property</mark> provided by an <mark style="color:orange;">ancestor view</mark>.
* [observer](/ios/swiftui/view/state/observable-object/observer.md)： <mark style="color:yellow;">any</mark> [view](/ios/swiftui/view.md) or <mark style="color:yellow;">object</mark> that <mark style="color:yellow;">uses the</mark> [observable object](/ios/swiftui/view/state/observable-object/observableobject.md)<mark style="color:yellow;">'s data</mark>.
* [data model](/ios/master/term/data-model.md)：data shared with any views in your app.
  {% endtab %}

{% tab title="📘 手冊" %}

* [SwiftUI](https://developer.apple.com/documentation/swiftui) ⟩ [Model data](https://developer.apple.com/documentation/swiftui/model-data) ⟩&#x20;
  * [EnvironmentObject](https://developer.apple.com/documentation/swiftui/environmentobject)
  * [.environmentObject(\_:)](https://developer.apple.com/documentation/swiftui/scene/environmentobject\(_:\))
* [Combine](https://developer.apple.com/documentation/combine) ⟩ [ObservableObject](https://developer.apple.com/documentation/combine/observableobject) (protocol)
* [SwiftUI](https://developer.apple.com/documentation/swiftui) ⟩ [Model data](https://developer.apple.com/documentation/swiftui/model-data) ⟩ [@ObservedObject](https://developer.apple.com/documentation/swiftui/observedobject) (property wrapper)
  {% endtab %}

{% tab title="📗 參考" %}

* [ ] Majid ⟩ [The difference between @StateObject, @EnvironmentObject, and @ObservedObject in SwiftUI](https://swiftwithmajid.com/2020/07/02/the-difference-between-stateobject-environmentobject-and-observedobject-in-swiftui/) #todo
  {% endtab %}
  {% endtabs %}
