Created
December 13, 2023 08:15
-
-
Save sisoje/e9965945558fa5cb3b6d28abc8e59f29 to your computer and use it in GitHub Desktop.
Wrap an optional item to a bool binding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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