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

# @Published

[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

{% hint style="success" %}
Use <mark style="color:purple;">`@Published`</mark> to declare a [published value](/ios/swiftui/view/state/observable-object/published-value.md) in an [observable object](/ios/swiftui/view/state/observable-object/observableobject.md) type.
{% 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
    }
}
```

{% hint style="danger" %}
The <mark style="color:purple;">`@Published`</mark> attribute is [class](/ios/swift/type/category/basic/class.md) <mark style="color:yellow;">constrained</mark>. <mark style="color:yellow;">Use it with</mark> [properties](/ios/swift/type/prop.md) <mark style="color:yellow;">of classes</mark>, <mark style="color:red;">not</mark> with <mark style="color:red;">non-class</mark> [types](/ios/swift/type.md) like <mark style="color:orange;">structures</mark>.
{% endhint %}

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

* [ObservableObject](/ios/swiftui/view/state/observable-object/observableobject.md)
* [published value](/ios/swiftui/view/state/observable-object/published-value.md)
* [@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 %}
