# published value

[SwiftUI](https://lochiwei.gitbook.io/ios/swiftui) ⟩ [view](https://lochiwei.gitbook.io/ios/swiftui/view) ⟩ [state](https://lochiwei.gitbook.io/ios/swiftui/view/state) ⟩ [observable object](https://lochiwei.gitbook.io/ios/swiftui/view/state/observable-object) ⟩ published value

{% hint style="success" %}
In an [observable object](https://lochiwei.gitbook.io/ios/swiftui/view/state/observable-object/observableobject), a [property](https://lochiwei.gitbook.io/ios/swift/type/prop) <mark style="color:yellow;">declared</mark> by adding <mark style="color:orange;">@Published</mark> [property wrapper](https://lochiwei.gitbook.io/ios/swift/type/prop/wrapper) that <mark style="color:yellow;">notifies</mark> all [observers](https://lochiwei.gitbook.io/ios/swiftui/view/state/observable-object/observer) <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](https://lochiwei.gitbook.io/ios/swiftui/view) 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](https://lochiwei.gitbook.io/ios/swiftui/view/state/observable-object/observableobject "mention")：declare an <mark style="color:yellow;">observable object</mark> [type](https://lochiwei.gitbook.io/ios/swift/type).
* [published](https://lochiwei.gitbook.io/ios/swiftui/view/state/observable-object/published "mention")：declare a <mark style="color:purple;">published value</mark> in an [observable object](https://lochiwei.gitbook.io/ios/swiftui/view/state/observable-object/observableobject) type.
* [stateobject](https://lochiwei.gitbook.io/ios/swiftui/view/state/object/stateobject "mention")：initialize an observable object.
  {% endtab %}

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

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/ios/swiftui/view/state/observable-object/published-value.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
