I would like to compare an enum value to an enum type without using switch. The following code, for example, works using the ~=
operator:
enum MyEnum {
case A, B
}
let myEnum = MyEnum.A
let isA = myEnum ~= MyEnum.A
isA
equals true
above.
However, when I try to compare an enum of an enum type with an associated value, like below, I get the compile error Binary operator '~=' cannot be applied to two MyEnum operands
.
enum MyEnum {
case A, B(object: Any)
}
let myEnum = MyEnum.A
let isA = myEnum ~= MyEnum.A
Is there a way to work around this error to use the ~=
pattern matching operator? Or is my only recourse the following syntax, which in my opinion is significantly more cumbersome:
enum MyEnum {
case A, B(object: Any)
}
let myEnum = MyEnum.A
let isA: Bool
switch myEnum {
case .A:
isA = true
default:
isA = false
}
Thanks in advance for your input and suggestions!