Generic Structure

Binding

A property wrapper type that can read and write a value owned by a source of truth.

Declaration

@frozen @propertyWrapper @dynamicMemberLookup struct Binding<Value>

Overview

Use a binding to create a two-way connection between a property that stores data, and a view that displays and changes the data. A binding connects a property to a source of truth stored elsewhere, instead of storing data directly. For example, a button that toggles between play and pause can create a binding to a property of its parent view using the @Binding property wrapper.

struct PlayButton: View {
    @Binding var isPlaying: Bool
    
    var body: some View {
        Button(action: {
            self.isPlaying.toggle()
        }) {
            Image(systemName: isPlaying ? "pause.circle" : "play.circle")
        }
    }
}

The parent view declares a property to hold the playing state, using the @State property wrapper to indicate that this property is the value’s source of truth.

struct PlayerView: View {
    var episode: Episode
    @State private var isPlaying: Bool = false
    
    var body: some View {
        VStack {
            Text(episode.title)
            Text(episode.showTitle)
            PlayButton(isPlaying: $isPlaying)
        }
    }
}

When PlayerView initializes PlayButton, it passes a binding of its state property into the button’s binding property. Applying the $ prefix to a property wrapped value returns its projectedValue, which for a state property wrapper returns a binding to the value.

Whenever the user taps the PlayButton, the PlayerView updates its isPlaying state.

Topics

Creating a Binding

init<V>(Binding<V>)

Creates a binding to an optional value.

init<V>(Binding<V>)

Creates a binding to a hashable value.

init?(Binding<Value?>)

Creates a binding to an unwrapped value.

init(get: () -> Value, set: (Value) -> Void)

Creates a binding with closures that read and write the binding value.

init(get: () -> Value, set: (Value, Transaction) -> Void)

Creates a binding with a closure that reads from the binding value, and a closure that applies a transaction when writing to the binding value.

static func constant(Value) -> Binding<Value>

Creates a binding with an immutable value.

Getting the Value

var wrappedValue: Value

The underlying value referenced by the binding variable.

var projectedValue: Binding<Value>

A projection of the binding value that returns a binding.

subscript<Subject>(dynamicMember: WritableKeyPath<Value, Subject>) -> Binding<Subject>

Returns a binding to the resulting value of a given key path.

Updating the Value

func update()

Updates the underlying value of the binding.

Applying Animations

func animation(Animation?) -> Binding<Value>

Specifies an animation to perform when the binding value changes.

Applying Transactions

func transaction(Transaction) -> Binding<Value>

Specifies a transaction for the binding.

var transaction: Transaction

The binding’s transaction.

Relationships

Conforms To

See Also

View State

struct State

A property wrapper type that can read and write a value managed by SwiftUI.