An open API service indexing awesome lists of open source software.

https://github.com/fcanas/bindbackstop

(Binding<T?>, T) -> Binding<T>
https://github.com/fcanas/bindbackstop

bindings optional optionals swift swiftui

Last synced: 10 months ago
JSON representation

(Binding<T?>, T) -> Binding<T>

Awesome Lists containing this project

README

          

# BindBackstop [![Build](https://github.com/fcanas/BindBackstop/actions/workflows/swift.yml/badge.svg?branch=canon)](https://github.com/fcanas/BindBackstop/actions/workflows/swift.yml)

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.

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

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

## License

BindBackstop is available under the [MIT license](LICENSE).