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>
- Host: GitHub
- URL: https://github.com/fcanas/bindbackstop
- Owner: fcanas
- License: mit
- Created: 2021-05-02T18:25:49.000Z (almost 5 years ago)
- Default Branch: canon
- Last Pushed: 2021-10-28T01:55:57.000Z (over 4 years ago)
- Last Synced: 2025-03-29T06:02:22.694Z (11 months ago)
- Topics: bindings, optional, optionals, swift, swiftui
- Language: Swift
- Homepage:
- Size: 4.88 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# BindBackstop [](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).