---
description: declare an environment object
---

# @EnvironmentObject

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

{% hint style="success" %}
The <mark style="color:purple;">`@EnvironmentObject`</mark> [property wrapper](../../../swift/type/prop/wrapper) is used to <mark style="color:yellow;">declare</mark> (in a [subview](../hierarchy/child)) an [environment object](broken-reference) 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](../../../master/term/data-model) <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](../state/observable-object/observableobject) to <mark style="color:yellow;">declare</mark> an <mark style="color:yellow;">observable object</mark> [type](../../../swift/type).
* Use [@Published](../state/observable-object/published) to <mark style="color:yellow;">declare</mark> [published values](../state/observable-object/published-value) in an [observable object](../state/observable-object/observableobject) type.
* Use [@StateObject](../state/object/stateobject) 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](../../../master/term/data-model) in the environment.
* Use <mark style="color:purple;">`@EnvironmentObject`</mark> to <mark style="color:yellow;">declare</mark> an [observable object](../state/observable-object/observableobject) <mark style="color:yellow;">property</mark> provided by an <mark style="color:orange;">ancestor view</mark>.
* [observer](../state/observable-object/observer)： <mark style="color:yellow;">any</mark> [view](..) or <mark style="color:yellow;">object</mark> that <mark style="color:yellow;">uses the</mark> [observable object](../state/observable-object/observableobject)<mark style="color:yellow;">'s data</mark>.
* [data model](../../../master/term/data-model)：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 %}
