---
description: ╱🚧 under construction
---

# @StateObject

[SwiftUI](../../..) ⟩ [view](../..) ⟩ [state](..) ⟩ [object]() ⟩ @StateObject

{% hint style="success" %}
Use <mark style="color:purple;">`@StateObject`</mark> to <mark style="color:yellow;">declare</mark> and <mark style="color:yellow;">initialize</mark> a [state object]() in a [view](../..) that [observes](../observable-object/observer) the object's [data](../../../../master/term/data-model).
{% endhint %}

```swift
// ⭐️ 1. declare a type that conforms to `ObservableObject`
class Zoo : ObservableObject {

    // ⭐️ 2. declare published value(s) in this type
    @Published var creatures = [
        Creature(name: "Gorilla", emoji: "🦍"),
        Creature(name: "Peacock", emoji: "🦚"),
    ]
    
}

// content view
struct ContentView: View {

    // ⭐️ 3. declare & initialize a state object
    @StateObject var data = Zoo()
    
    var body: some View {
        // 4. access the state object's data in subviews
    }
}
```

{% tabs %}
{% tab title="💈範例" %}
📗 參考：Swift Playgrounds (Keep Going with Apps) - Sharing Data Between Views.

📁 CreatureZoo&#x20;

```swift
// ⭐️ 1. declare an observable object type
class CreatureZoo : ObservableObject {
    // ⭐️ 2. declare published properties
    @Published var creatures = [
        Creature(name: "Gorilla", emoji: "🦍"),
        Creature(name: "Peacock", emoji: "🦚"),
    ]
}
```

📁 MyApp

```swift
@main
struct MyApp: App {
    
    // ⭐️ 3. intialize an observable object
    @StateObject var data = CreatureZoo()
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                // ⭐️ 4. put data in environment
                .environmentObject(data)
        }
    }
}
```

📁 ContentView

```swift
struct ContentView: View {
    
    // ⭐️ 5. inject data from environment
    @EnvironmentObject var data : CreatureZoo

    var body: some View {
        // ⭐️ 6. access data in subviews
    }
}
```
{% endtab %}

{% tab title="🔴 主題" %}
* [binding-from-stateobject](../binding/binding/binding-from-stateobject "mention")
{% endtab %}

{% tab title="📗 參考" %}
* [ ] [How to initialize @StateObject with parameters in SwiftUI](https://sarunw.com/posts/how-to-initialize-stateobject-with-parameters-in-swiftui/)
* [ ] Sarun ⟩ [Should we manually call @StateObject initializer](https://sarunw.com/posts/manually-initialize-stateobject/)
{% endtab %}

{% tab title="🔸 定義" %}
{% hint style="info" %}
<mark style="color:purple;">**StateObject**</mark> 是一個 [wrapper](../../../../swift/type/prop/wrapper "mention")，它內含的資料一定要遵循 [observableobject](../observable-object/observableobject "mention") 協定。
{% endhint %}

```swift
@frozen @propertyWrapper 
struct StateObject<ObjectType> where ObjectType : ObservableObject
```
{% endtab %}

{% tab title="📘 手冊" %}
* [SwiftUI](https://developer.apple.com/documentation/swiftui) ⟩&#x20;
  * View ⟩ [State Modifiers](https://developer.apple.com/documentation/swiftui/view-state) ⟩
    * [environmentObject(\_:)](https://developer.apple.com/documentation/swiftui/view/environmentobject\(_:\))
  * [State and Data Flow](https://developer.apple.com/documentation/swiftui/state-and-data-flow) ⟩
    * [StateObject](https://developer.apple.com/documentation/swiftui/stateobject) (<mark style="color:red;">**struct**</mark>)
    * [ObservedObject](https://developer.apple.com/documentation/swiftui/observedobject) (<mark style="color:red;">**struct**</mark>)
    * [EnvironmentObject](https://developer.apple.com/documentation/swiftui/environmentobject) (<mark style="color:red;">**struct**</mark>)
* [Combine](https://developer.apple.com/documentation/combine) ⟩&#x20;
  * [ObservableObject](https://developer.apple.com/documentation/Combine/ObservableObject) (protocol)
{% endtab %}

{% tab title="👥 相關" %}
* [observableobject](../observable-object/observableobject "mention")：declare an <mark style="color:yellow;">observable object</mark> [type](../../../../swift/type).
* [published](../observable-object/published "mention")：declare a [published value](../observable-object/published-value) in an [observable object](../observable-object/observableobject) type.
* <mark style="color:purple;">@StateObject</mark>：initialize an [observable object](../observable-object/observableobject).
* is [wrapper](../../../../swift/type/prop/wrapper "mention").
* wraps an object that conforms to [observableobject](../observable-object/observableobject "mention").
* [binding](../binding/binding "mention") can be [created](../binding/binding/binding-from-stateobject).
* [data model](../../../../master/term/data-model)：the [source of truth](../../../../master/term/source-of-truth) of your [app](../../../app).
{% endtab %}
{% endtabs %}

