SwiftUI Binding Initialize
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
@Binding is one of SwiftUI’s core tools for passing mutable state from a parent view into a child view. It lets the child read and write a value that is owned somewhere else, which is essential for controls such as toggles, forms, and editors.
Most of the confusion around “binding initialize” comes from custom initializers. A @Binding property is not assigned the same way as a normal stored property, so the initializer has to target the binding storage correctly.
Basic @Binding Usage
The simplest case does not need a custom initializer at all.
The parent owns the source of truth through @State, and the child receives a binding using the $ prefix.
Why Custom Initialization Feels Different
Suppose your child view needs both a binding and a normal parameter. A common first attempt is this:
That fails because isEnabled is the wrapped value, not the binding storage. The correct target is the underscored storage property.
The Correct Pattern for Initializing a Binding
Use _propertyName inside the initializer.
The key line is:
That assigns the incoming Binding<Bool> to the property-wrapper storage instead of trying to assign it to the wrapped Bool value.
Using the View from a Parent
The parent still passes the binding with $:
This is the standard parent-child data flow in SwiftUI. The child edits the parent’s state without owning it.
Constant Bindings for Previews and Read-Only Cases
Sometimes you want a view that requires a binding, but you are using it in a preview or test where no live state exists. In that case, use Binding.constant.
A constant binding is useful for previews, but it is read-only in practice. The UI may render, but changes do not propagate back to mutable state because there is none.
Initializing Derived Bindings
You can also create a binding manually from getter and setter closures. This is useful when the child view should edit part of a larger model.
This is more advanced, but it shows that a Binding is a value with read and write behavior, not just syntax sugar.
Common Pitfalls
The most common mistake is trying to assign a Binding<T> directly to a @Binding var in a custom initializer. The fix is to assign to the underscored storage property, such as _isEnabled.
Another issue is forgetting the $ when passing a state variable from the parent. isEnabled is the value, while $isEnabled is the binding.
It is also easy to use .constant(...) in places where you actually expect edits to persist. Constant bindings are useful for previews, but they are not a substitute for real mutable state.
Finally, choose @Binding only when the child should edit state owned by another view. If the child owns the value itself, @State is usually the right tool instead.
Summary
- '
@Bindinglets a child view read and write state owned by another view.' - In a custom initializer, assign the incoming binding to the underscored storage property such as
_isEnabled. - Parents pass bindings using the
$prefix on@Statevalues. - '
Binding.constant(...)is useful for previews and non-editable scenarios.' - Use
@Bindingonly when the child should mutate external state rather than own the state itself.
Related reading
- SwiftUI can't tap in Spacer of HStack
- SwiftUI can't tap in Spacer of HStack
- SwiftUI. How to change the placeholder color of the TextField?
- SwiftUI How to implement a custom init with Binding variables
- SwiftUI NavigationLink loads destination view immediately, without clicking
- SwiftUI NavigationView navigationBarTitle LayoutConstraints issue
- SwiftUI State var initialization issue
- SwiftUI Status bar color
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.