BetterBinding aims to improve SwiftUI's Binding
by introducing several operators that will save you time when writing SwiftUI views.
BetterBinding can be installed via Swift Package Manager:
https://github.com/benlmyers/better-binding
BetterBinding has 5 binding-related operators you can use to make using Binding
in your projects much quicker.
Define a constant using the %
operator:
%false // .constant(false)
%variable // .constant(variable)
%(-2.0) // .constant(-2.0)
Unwrap a binding using the %
operator:
%myBindingVar // myBindingVar.wrappedValue
Unwrap Binding<Optional<T>>
values conditionally:
$myBindingOptionalValue ?? %true
Unwrap Binding<Optional<T>>
values forcefully:
!$myBindingOptionalValue
Check the equality of a Binding<T>
's unwrapped value with a T
value, and get a Binding<Bool>
source of truth:
Toggle("Option 0", isOn: $optionValue == 0)
Toggle("Option 1", isOn: $optionValue == 1)
Toggle("Option 2", isOn: $optionValue != 2)
Compare a Binding<T>
with a T
value, and get a Binding<Bool>
source of truth:
Toggle("Option 0", isOn: $optionValue < 2)
Toggle("Option 1", isOn: $optionValue >= 5)
Use binary locical operators to unify two Binding<Bool>
s:
Toggle(“A or B”, isOn: $valueA || $valueB) // Toggling this will set both to the new value.
Toggle(“A and B”, isOn: $valueA && $valueB) // Toggling this will set both to the new value.
Negate a Binding<Bool>
:
Toggle("Not A", isOn: !$valueA)