Created
September 11, 2024 15:29
-
-
Save shaps80/5615c8a71fe26d229bf063d2e7c87a5c to your computer and use it in GitHub Desktop.
Simplifies binding option set values to a SwiftUI View.
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 | |
public extension Binding where Value: OptionSet { | |
func toggling(_ value: Value.Element) -> Binding<Bool> { | |
.init( | |
get: { | |
wrappedValue.contains(value) | |
}, | |
set: { | |
if $0 { | |
wrappedValue.insert(value) | |
} else { | |
wrappedValue.remove(value) | |
} | |
} | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example:
Toggling the control, will insert/remove the
.bottom
value from the option set. Can be used with anyOptionSet
.