State and Data Flow

Control and respond to the flow of data and changes within your app’s models.

Overview

States and bindings connect views to your app’s underlying data model. When you declare a state, SwiftUI stores it for you and manages the state’s connections to its view. When the state updates, the view invalidates its appearance and updates itself. You can also connect animations to the state to animate how the view portrays the change.

Create bindings from members of your state to connect to individual views. Bindings offer two-way connections, so that onscreen controls can mutate the state. Bindings also have transactions to pass values between views.

Topics

Essentials

Handling User Input

In the Landmarks app, a user can flag their favorite places, and filter the list to show just their favorites. To create this feature, you’ll start by adding a switch to the list so users can focus on just their favorites, and then you’ll add a star-shaped button that a user taps to flag a landmark as a favorite.

Bindings

struct Binding

A manager for a value that provides a way to mutate it.

Data-Dependent Views

struct State

A persistent value of a given type, through which a view reads and monitors the value.

struct EnvironmentObject

A dynamic view property that uses a bindable object supplied by an ancestor view to invalidate the current view whenever the bindable object changes.

protocol DynamicProperty

A stored variable that updates an external property of a view.

Environment Values

struct Environment

A dynamic view property that reads a value from the view’s environment.

struct EnvironmentValues

A collection of environment values.

Preferences

protocol PreferenceKey

A named value produced by a view.

struct LocalizedStringKey

The key used to look up a string in a strings file or strings dictionary file.

Transactions

struct Transaction

The context of the current state-processing update.

See Also

Data and Events

Gestures

Define interactions from taps, clicks, and swipes to fine-grained gestures.