NEW: Join my free 100 Days of SwiftUI challenge today! >>

What is the @Binding property wrapper?

Paul Hudson    @twostraws   

Fully updated for Xcode 11.2

@Binding is one of SwiftUI’s less used property wrappers, but it’s still hugely important: it lets us declare that one value actually comes from elsewhere, and should be shared in both places. This is not the same as @ObservedObject or @EnvironmentObject, both of which are designed for reference types to be shared across potentially many views.

For example, we might have a ContentView with an @State property storing whether a child view is being presented or not, like this:

struct ContentView: View {
    @State private var showingAddUser = false

    var body: some View {
        VStack {
            // your code here
        }
    }
    .sheet(isPresented: $showingAddUser) {
        // show the add user view
    }
}

That uses showingAddUser for the isPresented parameter of our sheet, which means when that Boolean becomes true the add user view will be shown. However, how can we allow the add user view to dismiss itself if it needs to – if the user taps a Done button, for example?

What we want to happen is for the add user view to set showingAddUser back to false, which will cause ContentView to hide it. This is exactly what @Binding is for: it lets us create a property in the add user view that says “this value will be provided from elsewhere, and will be shared between us and that other place.”

So, we might create an add user view like this:

struct AddView: View {
    @Binding var isPresented: Bool

    var body: some View {
        Button("Dismiss") {
            self.isPresented = false
        }
    }
}

That property literally means “I have a Boolean value called isPresented, but it’s being stored elsewhere.” So, when we create that AddView to replace the // show the add user view comment from earlier, we’d need to provide the value so it can be manipulated:

.sheet(isPresented: $showingAddUser) {
    AddView(isPresented: self.$showingAddUser)
}

This allows both ContentView and AddView to share the same Boolean value – when it changes in one place it also changes in the other.

SPONSORED Instabug helps you identify and resolve severe crashes quickly. You can retrace in-app events and know exactly which line of code caused the crash along with environment details, network logs, repro steps, and the session profiler. Ask more questions or keep users up-to-date with in-app replies straight from your dashboard. Instabug takes data privacy seriously, so no one sees your data but you! See more detailed features comparison and try Instabug's crash reporting SDK for free.

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5