Skip to content

Instantly share code, notes, and snippets.

@davbeck
Created June 4, 2021 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davbeck/c970960ed72b1f54596ec0fd6a4e7ab0 to your computer and use it in GitHub Desktop.
Save davbeck/c970960ed72b1f54596ec0fd6a4e7ab0 to your computer and use it in GitHub Desktop.
import SwiftUI
func ?? <T>(optional: Binding<T?>, defaultValue: @escaping @autoclosure () -> T) -> Binding<T> {
return Binding {
optional.wrappedValue ?? defaultValue()
} set: { newValue in
optional.wrappedValue = newValue
}
}
func ?? <T>(optional: Binding<T?>, defaultValue: @escaping @autoclosure () -> T?) -> Binding<T?> {
return Binding {
optional.wrappedValue ?? defaultValue()
} set: { newValue in
optional.wrappedValue = newValue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment