Is there a way to abbreviate the following type of condition in Swift?
if ( (myEnum == .1 || myEnum == .2 || myEnum == .3 || myEnum == .8) && startButton.isSelected )
I tried typing:
if ( (myEnum == .1 || .2 || .3 || .8) && startButton.isSelected )
and:
if ( (myEnum == .1,.2,.3,.8) && startButton.isSelected )
but none of those worked. I also tried looking at documentation but can't find a similar example.
Thanks!
For this case I like to use John Sundell's extension to equatable:
You can then use it as:
I don't think there is a way to abbreviate it like you want but there is, perhaps, another way to approach the same thing...
By doing this your condition at the call site becomes...
By using this approach your code become more readable too. You can now give your condition a sensible name which other developers (including your future self) will be able to understand. Where before I would have no idea why those cases were chosen.
It also allows you to use this condition in multiple places and have only one implementation of it. So if the requirement changes you can change it in one place.