HeadlinesBriefing favicon HeadlinesBriefing.com

SwiftUI @Observable simplifies shared state

DEV Community •
×

Apple’s new @Observable macro, shipped in the Observation framework, lets a class conform to the Observable protocol and automatically publishes changes for all its stored properties. In a simple SwiftUI view, an ApplicationData object declares `title` and `input`; pressing a button updates `title` and the UI refreshes without any @State annotations.

To expose a two‑way binding, developers wrap the model with @Bindable. The wrapper lets a `TextField` bind directly to `appData.input`, and a save button copies that value into `title`. Larger screens often split shared data into a global ApplicationData and view‑specific ContentViewModel, each marked with @Observable for independent updates.

Sometimes a property should not trigger a redraw; marking it with @ObservationIgnored silences the publisher, as shown by a counter that increments without UI change. When a view must refresh manually, developers assign a fresh UUID via `id(_:)`. Watch for broader adoption of these wrappers as SwiftUI apps move toward cleaner state management.