How do I manually init a Swift Binding, with a desired wrapped value?
Using Bool as type, here is most basic desired init
let b: Binding<Bool> = Binding.init(true) //errors out with messages:
Cannot convert value of type 'Bool' to expected argument type 'Binding<Bool?>
Value of optional type 'Binding<Bool>?' must be unwrapped to a value of type 'Binding<Bool>'
Questions I have
- Why does the above error out?
- What is the correct syntax to init a simple Bool binding
- Why must optionals be involved in the type signature (<'Binding<Bool?>)
Binding<Bool>.constant(true)?Binding.init(Boolean)isn't a value initializer forBinding. Can you elaborate on what you're trying to accomplish? A binding is usually used for two-way communication in SwiftUI or (possibly) Combine. Initializing a Binding without a@Stateor@Publishedproperty behind it (or something else that gets written to in a custom Binding) is suspicious. Maybe if you describe what you're trying to accomplish, someone can help you find a solution.@State var bool: Bool = trueappears to vend aBinding<Bool>. I was trying to manually init a Binding<Bool> with a wrappedValue to see if I understood how they get created.