Binding variables can be created in the following ways:
@Statevariable’s projected value provides aBinding<Value>@ObservedObjectvariable’s projected value provides a wrapper from which you can get theBinding<Subject>for all of it’s properties- Point 2 applies to
@EnvironmentObjectas well. - You can create a Binding variable by passing closures for getter and setter as shown below:
let button = SaleButton(isOn: .init(get: { car.isReadyForSale },
set: { car.isReadyForSale = $0} ))
Note:
- As @nayem has pointed out you need
@State/@ObservedObject/@EnvironmentObject/@StateObject(added in SwiftUI 2.0) in the view for SwiftUI to detect changes automatically. - Projected values can be accessed conveniently by using
$prefix.