NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

What is the @Binding property wrapper?

Paul Hudson    @twostraws   

Updated for Xcode 14.2

@Binding 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") {
            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: $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.

Hacking with Swift is sponsored by Essential Developer

SPONSORED From March 20th to 26th, you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

Buy Pro Swift Buy Pro SwiftUI 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 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 Beyond Code

Was this page useful? Let us know!

Average rating: 4.1/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.