ObservableObject: Making Data Drive SwiftUI Views

ObservableObject lets your custom data classes signal SwiftUI views to refresh when data changes. Use it for reference types like a user profile or settings manager. Changes are ignored unless you mark properties with `@Published`, leaving your UI stale.
WHY IT EXISTS SwiftUI's declarative nature requires a clear signal to know when to re-render a view. While @State handles simple, local value types, it's insufficient for complex, shared data models. ObservableObject was created to bridge this gap, providing a standardized way for shared class instances (reference types) to announce that their data has changed, triggering a UI update.
THE MENTAL MODEL Think of an ObservableObject as a radio station and your SwiftUI views as radios. The object is the station, broadcasting updates whenever its important data changes. Your views are tuned into that station. When the object broadcasts a change, all listening views automatically hear it and update their appearance to reflect the new data. The @Published property wrapper is the 'On Air' light that signals a broadcast is happening.
HOW IT WORKS You conform a class to the ObservableObject protocol. Inside the class, you annotate properties with the @Published property wrapper. This wrapper automatically makes the property publish its changes. In your SwiftUI view, you declare a property to hold an instance of this class using a wrapper like @StateObject (to create and own the object) or @ObservedObject (to observe an existing object). When a @Published property's value changes, it notifies all subscribing views, causing SwiftUI to re-render their body.
WHEN TO USE IT Use ObservableObject for reference types (classes) that act as a single source of truth for a view or group of views. It's ideal for view models that manage network requests, data formatting, and business logic. It's also perfect for objects representing shared app state, like a user session or application settings, that need to be accessed and modified from multiple places.
WHEN NOT TO USE IT Avoid ObservableObject for simple value types (structs, enums, Ints) that are confined to a single view; @State is more lightweight and appropriate for that purpose. If you are just passing data down a view hierarchy without any need for mutation or observation, a standard property is more efficient. ObservableObject is specifically for classes whose state changes must be observed.
ONE CANONICAL EXAMPLE A UserSettings class managing an app's preferences is a classic use case. The class conforms to ObservableObject, and its properties, like isDarkModeEnabled and username, are marked @Published. A SettingsView would then hold an instance of UserSettings using @ObservedObject. When the user flips a toggle in the UI bound to isDarkModeEnabled, the property's value changes, the @Published wrapper sends an update, and SwiftUI automatically redraws any part of the UI that depends on that setting.
Read the original → developer.apple.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.