Binding to the wrapped value instead of the property wrapper’s storage. Use the synthesized underscore-prefixed property (e.g., self._amount = amount) to assign the binding directly in your custom initializer.Why this happens
When you declare @Binding var amount: Double, Swift synthesizes a private storage property named _amount that holds the Binding<Double> instance. The amount property itself is a computed getter/setter that reads and writes through that binding.
In a custom init, writing self.amount = amount tries to assign a Binding<Double> to a Double — hence the compiler error “Cannot assign value of type ‘Binding’ to type ‘Double’”. You must assign to the synthesized storage (_amount) instead of the wrapped value (amount).
Additionally, any @State property that derives its value from the binding (like includeDecimal in the example) should be a computed property. Initializing @State from init triggers “Accessing State outside View.body” at runtime because the view’s body hasn’t been created yet.
How to fix it
1. Assign to the underscore-prefixed storage property
Use self._amount = amount (Swift 5.5+, Xcode 13+) to pass the binding into the property wrapper’s storage:
struct AmountView: View {
@Binding var amount: Double
private var includeDecimal: Bool {
round(amount) - amount > 0
}
init(amount: Binding<Double>) {
self._amount = amount
}
var body: some View {
Text(includeDecimal ? "Has decimals" : "Whole number")
}
}
The underscore-prefixed property (_amount) is the synthesized storage for the @Binding wrapper. Assigning to it stores the binding so the wrapper can forward reads and writes.
2. Use the parameter’s .value for derived initial values
If you need to compute an initial value from the incoming binding, read amount.wrappedValue (or .value on the Binding parameter):
init(amount: Binding<Double>) {
self._amount = amount
// includeDecimal is computed, so no init assignment needed
}
Because includeDecimal is now a computed property, it always reflects the current amount — no manual update required.
3. Alternative: different parameter name for clarity
Naming the parameter differently avoids shadowing and makes it obvious which is the binding and which is the property:
struct AmountView: View {
@Binding var amount: Double
private var includeDecimal: Bool {
round(amount) - amount > 0
}
init(withAmount: Binding<Double>) {
self._amount = withAmount
}
var body: some View { /* ... */ }
}
This style is especially helpful in views with multiple bindings.
How to prevent it
- Always assign bindings to the underscore-prefixed storage (
_propertyName) in custom initializers. - Make derived values computed properties instead of
@State— they stay in sync automatically. - Avoid initializing
@Statefrominit; use computed properties oronAppearfor side effects. - Name initializer parameters distinctly from property names to prevent shadowing confusion.
FAQ
Why does @State cause a runtime crash when set in init?
@State storage is allocated when the view’s body is first evaluated. Accessing it before body runs (i.e., in init) triggers the fatal error “Accessing State outside View.body”. Use a computed property for values derived from other sources.
Can I use self.$amount = amount instead?
$amount (the projected value) works in older SwiftUI betas, but the underscore-prefixed storage (_amount) is the stable, documented way to initialize a property wrapper’s storage directly. Prefer _amount for current Xcode versions.
What if I have multiple @Binding properties?
Assign each one to its corresponding underscore storage in the initializer:
init(amount: Binding<Double>, currency: Binding<String>) {
self._amount = amount
self._currency = currency
}
Each @Binding gets its own synthesized _property storage.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.