> 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/object.md).

# environment object

[SwiftUI](/ios/swiftui.md) ⟩ [view](/ios/swiftui/view.md) ⟩ [environment](/ios/swiftui/view/environment.md) ⟩ object

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

1. <mark style="color:yellow;">initialized</mark> in an <mark style="color:orange;">ancester view</mark> by adding a [@StateObject](/ios/swiftui/view/state/object/stateobject.md) 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](/ios/swiftui/view/hierarchy/child.md), <mark style="color:yellow;">declare</mark> a corrspoinding [@EnvironmentObject](/ios/swiftui/view/environment/environmentobject.md) 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](/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="👥 相關" %}

* [ObservableObject](/ios/swiftui/view/state/observable-object/observableobject.md)：<mark style="color:yellow;">declare</mark> an <mark style="color:yellow;">observable object</mark> [type](/ios/swift/type.md).
* [@Published](/ios/swiftui/view/state/observable-object/published.md)：<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.
* [@StateObject](/ios/swiftui/view/state/object/stateobject.md)：<mark style="color:yellow;">initialize</mark> an [observable object](/ios/swiftui/view/state/observable-object/observableobject.md).
* 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 [@EnvironmentObject](/ios/swiftui/view/environment/environmentobject.md) 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 %}
