🔸published value
// ⭐️ 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
}
}Last updated