Defining a simple OptionSet:
public struct TestSet : OptionSet, Hashable
{
public let rawValue: Int
public init(rawValue:Int){ self.rawValue = rawValue}
public var hashValue: Int {
return self.rawValue
}
public static let A = TestSet(rawValue: 1 << 0)
public static let B = TestSet(rawValue: 1 << 1)
public static let C = TestSet(rawValue: 1 << 2)
}
Call it:
let ostest : TestSet = [.A, .B]
switch ostest{
case .A: print("A")
case .B: print("B")
case .C: print("C")
default: print("Default")
}
if ostest.contains(.A){
print("Contains A")
}
if ostest.contains(.B){
print("Contains B")
}
Output is:
Default
Contains A
Contains B
Is there a way to check if OptionSets contain a value or combination of values with a switch statement? It would be much cleaner than a series of if-contains statements.
You can't do this directly because switch is using Equatable and, I think, is using SetAlgebra.
However, you can wrap the OptionSet with something like:
public struct TestSetEquatable<T: OptionSet>: Equatable {
let optionSet: T
public static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.optionSet.isSuperset(of: rhs.optionSet)
}
}
Which lets you do:
let ostest : TestSet = [.A, .C]
switch TestSetEquatable(optionSet: ostest) {
case TestSetEquatable(optionSet: [.A, .B]):
print("-AB")
fallthrough
case TestSetEquatable(optionSet: [.A, .C]):
print("-AC")
fallthrough
case TestSetEquatable(optionSet: [.A]):
print("-A")
fallthrough
default:
print("-")
}
This prints:
-AC
-A
- // from the fall through to default
Opinion: I'm not inclined to do use this code myself but if I had to, this is what I would do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With