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.

Beta
protocol BindingConvertible

A type that can provide a binding to its stored value.

Beta

Data-Dependent Views

protocol DynamicViewProperty

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

Beta
struct State

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

Beta
protocol BindableObject

An object that serves as a view’s model.

Beta
struct ObjectBinding

A dynamic view property that subscribes to a bindable object automatically, invalidating the view when it changes.

Beta
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.

Beta

Environment Values

struct Environment

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

Beta
struct EnvironmentValues

A collection of environment values.

Beta

Navigation Models

struct DynamicNavigationDestinationLink

A dynamic view property that represents a collection of possible destinations in a navigation stack.

Beta

Preferences

protocol PreferenceKey

A named value produced by a view.

Beta
struct LocalizedStringKey

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

Beta

Transactions

struct Transaction

The context of the current state-processing update.

Beta

See Also

Data and Events

Gestures

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

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software