BindBackstop

1.0.0

(Binding<T?>, T) -> Binding<T>
fcanas/BindBackstop

What's New

1.0

2021-10-29T01:17:44Z

This is the first and quite possibly the only release of BindBackstop. The code is so simple, and it's been in stable use for months. I haven't wanted it to do anything more.

BindBackstop

For SwiftUI's Bindings, make a non-optional Binding from an optional one, given a default value.

This is all the code. It's only published here because I've reinvented this too many times.

extension Binding {
    init(_ source: Binding<Value?>, _ defaultValue: Value) {
        self.init(get: { () -> Value in
            return source.wrappedValue ?? defaultValue
        }) { (value) in
            source.wrappedValue = value
        }
    }
}

extension Binding {
    func with<T>(default v: T) -> Binding<T> where Value == T? {
        return Binding<T>(self, v)
    }
}

License

BindBackstop is available under the MIT license.

Description

  • Swift Tools 5.3.0
View More Packages from this Author

Dependencies

  • None
Last updated: Tue Mar 19 2024 16:58:42 GMT-0900 (Hawaii-Aleutian Daylight Time)