‹ back

Understanding @Binding in SwiftUI

April 23, 2022



One of the key features of SwiftUI is the @Binding property wrapper, which allows for two-way communication between views and their parent views.

In this blog post, we will explore @Binding in SwiftUI and how it can be used to create dynamic and interactive user interfaces.

What is @Binding?

@Binding is a property wrapper in SwiftUI that creates a two-way connection between a property defined in a child view and a property defined in its parent view. This means that changes made to the child view’s property will be reflected in the parent view’s property, and vice versa.

Here is an example of how @Binding can be used to create a simple counter app:

struct ContentView: View {
    @State private var count = 0

    var body: some View {
        VStack {
            Text("\\(count)")
                .font(.largeTitle)
            CounterView(count: $count)
        }
    }
}

struct CounterView: View {
    @Binding var count: Int

    var body: some View {
        HStack {
            Button("-") {
                count -= 1
            }
            Button("+") {
                count += 1
            }
        }
    }
}

In the above example, the ContentView defines a @State property called count, which is initially set to 0. It then creates a CounterView and passes the count property using the $ prefix to create a binding.

The CounterView defines a @Binding property called count, which is used to display the current count and to update it when the user taps the “+” or “-” buttons.

How @Binding works

When a child view is created with a @Binding property, SwiftUI automatically creates a binding between the child view’s property and the parent view’s property. This binding is essentially a reference to the parent view’s property, which allows the child view to read and modify its value.

When the child view modifies the @Binding property, SwiftUI updates the parent view’s property with the new value. This triggers a re-render of the parent view, which in turn updates the child view with the latest value of the @Binding property.

Best practices for using @Binding

Here are some best practices for using @Binding in your SwiftUI apps:

  • Use @Binding sparingly: While @Binding can be a powerful tool for creating dynamic and interactive user interfaces, it can also make your code more complex and harder to reason about. Use @Binding only when it’s necessary to create a two-way connection between views.
  • Use descriptive names for @Binding properties: When creating a @Binding property, use a descriptive name that reflects the purpose of the property. This makes it easier to understand the role of the property in the context of the view hierarchy.
  • Avoid circular dependencies: When using @Binding, be careful not to create circular dependencies between views. This can lead to infinite loops and crashes.
  • Use @State or @ObservedObject instead of @Binding when appropriate: In some cases, it may be more appropriate to use @State or @ObservedObject instead of @Binding. For example, if the property you want to pass to a child view is only used for display purposes and not for updating the parent view, then it’s better to use @State or @ObservedObject.

Conclusion

@Binding is a powerful property wrapper in SwiftUI that allows for two-way communication between views and their parent views.

It’s a great tool for creating dynamic and interactive user interfaces, but it should be used sparingly and with care.

By following best practices and using @Binding appropriately, you can create SwiftUI apps that are both elegant and easy to reason about.

Thank you for reading this far.

Share on Twitter
Mastodon