> 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/state/observable-object/published-value.md).

# published value

[SwiftUI](/ios/swiftui.md) ⟩ [view](/ios/swiftui/view.md) ⟩ [state](/ios/swiftui/view/state.md) ⟩ [observable object](/ios/swiftui/view/state/observable-object.md) ⟩ published value

{% hint style="success" %}
In an [observable object](/ios/swiftui/view/state/observable-object/observableobject.md), a [property](/ios/swift/type/prop.md) <mark style="color:yellow;">declared</mark> by adding <mark style="color:orange;">@Published</mark> [property wrapper](/ios/swift/type/prop/wrapper.md) that <mark style="color:yellow;">notifies</mark> all [observers](/ios/swiftui/view/state/observable-object/observer.md) <mark style="color:yellow;">when its value is</mark> <mark style="color:red;">about</mark> <mark style="color:yellow;">to change</mark>. When the value changes, SwiftUI <mark style="color:yellow;">updates</mark> all [views](/ios/swiftui/view.md) that <mark style="color:yellow;">use its data</mark>. Also called a <mark style="color:purple;">published property</mark>.
{% endhint %}

```swift
// ⭐️ 1. declare an observable object type
class CreatureZoo : ObservableObject {

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

// content view
struct ContentView: View {

    // ⭐️ 3. initialize an observable object
    @StateObject var data = CreatureZoo()
    
    var body: some View {
        // ⭐️ 4. access `data.creatures` in subviews
    }
}
```

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

* [ObservableObject](/ios/swiftui/view/state/observable-object/observableobject.md)：declare an <mark style="color:yellow;">observable object</mark> [type](/ios/swift/type.md).
* [@Published](/ios/swiftui/view/state/observable-object/published.md)：declare a <mark style="color:purple;">published value</mark> in an [observable object](/ios/swiftui/view/state/observable-object/observableobject.md) type.
* [@StateObject](/ios/swiftui/view/state/object/stateobject.md)：initialize an observable object.
  {% endtab %}

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

* [Combine](https://developer.apple.com/documentation/combine) ⟩ [Published](https://developer.apple.com/documentation/combine/published)
  {% endtab %}
  {% endtabs %}
