> 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/master/term/data-model.md).

# data model

[terms](/ios/master/term.md) ⟩ data model &#x20;

{% hint style="success" %} <mark style="color:yellow;">To share the same data between multiple</mark> [views](/ios/swiftui/view.md), you need a single [source of truth](/ios/master/term/source-of-truth.md) that is [separate](/ios/master/term/data-separation.md) <mark style="color:yellow;">from the views</mark>. This is the <mark style="color:purple;">data model</mark>, and you can share it with any views that need access to the data.
{% endhint %}

* Your [user interface](/ios/master/term/user-interface.md) may <mark style="color:yellow;">display</mark> data from the <mark style="color:purple;">data model</mark>, and may [interact](/ios/master/term/user-interaction.md) with it to <mark style="color:yellow;">modify</mark> the data.&#x20;
* A common way to construct a <mark style="color:purple;">data model</mark> for a SwiftUI [app](/ios/swiftui/app.md) is to use an [observable object](/ios/swiftui/view/state/observable-object/observableobject.md). an [observable object](/ios/swiftui/view/state/observable-object/observableobject.md) is an object (<mark style="color:orange;">reference type</mark>) that <mark style="color:yellow;">multiple views can observe</mark>.

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

* [@StateObject](/ios/swiftui/view/state/object/stateobject.md)
* [ObservableObject](/ios/swiftui/view/state/observable-object/observableobject.md)
* [view state](/ios/swiftui/view/state.md)
  {% endtab %}
  {% endtabs %}

```swift
import SwiftUI

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

struct Creature : Identifiable { ... }
```
