Member-only story
The @Binding property wrapper
Two way binding
Although I’ve labeled this article as beginner, I’ve noticed that my previous article about property wrappers just didn’t make it seem so. So I think it really asks for a new article about the @Binding
property wrapper.
The image? Books are bound, things are bound together. I guess that is the root of the meaning of @Binding
, bit we will get there in this article if you just read on!
Difficulty: Beginner | Easy | Normal | Challenging
Prerequisites:
Terminology
Property Wrapper: A property wrapper adds a layer of separation between code that manages how a property is stored and the code that defines a property
@Binding: The @Binding
property wrapper enables a two-way connection between a property which stores data and a view that displays and mutates that data.
The @Binding property wrapper
In Swift, @Binding is used for a two-way connection between a property and a view. When you need to pass a value type from a parent view to a child view (and wish the child view to mutate the value) the @Binding
keyword is the answer to the problem.