Question or problem in the Swift programming language:
If I have set my cases in enum, can I call multiple of those cases in a switch statement? aka case .a, .b: return true
enum myLetters { case a case b case c var myCondition: Bool { switch self { case .a, .b: return true case .c: return false default: return false } } }
How to solve the problem:
Solution 1:
Yes, take a look at Swift’s documentation on switch
statement.
In order to achieve what you want, you need to check for the current value of myLetters
:
var myCondition: Bool { switch self { case .a, .b: return true case .c: return false } }
Solution 2:
If you want to group cases with the same assosiated value, you can do the following:
var myCondition: Bool { switch self { case .a(let value), .b(let value): return value case .c(let value1, let value2): // do stuff with value1 and value 2 } }
Unfortunately you can’t currently combine the let
statements to a single one.