Skip to content

Instantly share code, notes, and snippets.

@sisoje
Created December 13, 2023 08:15
Show Gist options
  • Save sisoje/e9965945558fa5cb3b6d28abc8e59f29 to your computer and use it in GitHub Desktop.
Save sisoje/e9965945558fa5cb3b6d28abc8e59f29 to your computer and use it in GitHub Desktop.
Wrap an optional item to a bool binding
import SwiftUI
extension Binding {
static func boolify<T: Any>(_ binding: Binding<T?>) -> Binding<Bool> {
Binding<Bool> {
binding.wrappedValue != nil
} set: { newValue in
guard !newValue else {
assertionFailure()
return
}
binding.wrappedValue = nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment