# environment object

[SwiftUI](../..) ⟩ [view](..) ⟩ [environment]() ⟩ object

{% hint style="success" %}
An <mark style="color:purple;">environment object</mark> is a [state object](../state/object), which is&#x20;

1. <mark style="color:yellow;">initialized</mark> in an <mark style="color:orange;">ancester view</mark> by adding a [@StateObject](../state/object/stateobject) property&#x20;
2. and <mark style="color:yellow;">put in the environment</mark> by calling the <mark style="color:orange;">ancester view</mark>'s [.environmentObject(\_:)](https://developer.apple.com/documentation/swiftui/scene/environmentobject\(_:\)) modifier.

To use an <mark style="color:purple;">environment object</mark> In a [subview](../hierarchy/child), <mark style="color:yellow;">declare</mark> a corrspoinding [@EnvironmentObject](environmentobject) property in the subview.
{% endhint %}

```swift
// app
struct MyApp: App {
    
    // ⭐️ 1. declare & initialize a state object
    @StateObject private var data = ModelData() 
    
    var body: some Scene {
        WindowGroup {
            RootView()
                // ⭐️ 2. put an environment object.
                .environmentObject(data)         
        }
    }
}

// Subview: RootView's subview
struct Subview: View {

    // ⭐️ get the environment object.
    @EnvironmentObject var data: 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="👥 相關" %}
* [observableobject](../state/observable-object/observableobject "mention")：<mark style="color:yellow;">declare</mark> an <mark style="color:yellow;">observable object</mark> [type](../../../swift/type).
* [published](../state/observable-object/published "mention")：<mark style="color:yellow;">declare</mark> [published values](../state/observable-object/published-value) in an [observable object](../state/observable-object/observableobject) type.
* [stateobject](../state/object/stateobject "mention")：<mark style="color:yellow;">initialize</mark> an [observable object](../state/observable-object/observableobject).
* Use [.environmentObject(\_:)](https://developer.apple.com/documentation/swiftui/scene/environmentobject\(_:\)) to put the [model object](../../../master/term/data-model) in the environment.
* Use [@EnvironmentObject](environmentobject) 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 %}
