Model data
Manage the data that your app uses to drive its interface.
Overview
SwiftUI offers a declarative approach to user interface design. As you compose a hierarchy of views, you also indicate data dependencies for the views. When the data changes, either due to an external event or because of an action that the user performs, SwiftUI automatically updates the affected parts of the interface. As a result, the framework automatically performs most of the work that view controllers traditionally do.
[Image]
The framework provides tools, like state variables and bindings, for connecting your app’s data to the user interface. These tools help you maintain a single source of truth for every piece of data in your app, in part by reducing the amount of glue logic you write. Select the tool that best suits the task you need to perform:
Manage transient UI state locally within a view by wrapping value types as State() properties.
Share a reference to a source of truth, like local state, using the Binding property wrapper.
Connect to and observe reference model data in views by applying the Observable() macro to the model data type. Instantiate an observable model data type directly in a view with a State() property. Share the observable model data with other views in the hierarchy without passing a reference using the Environment property wrapper.
Topics
Creating and sharing view state
Creating model data
Managing model data in your appMigrating from the Observable Object protocol to the Observable macroObservable()Monitoring data changes in your appStateObjectObservedObjectObservableObject