Frankie Frankie - 5 months ago 9
iOS Question

Swift: if case pattern match for multiple OR conditions

Trying to come up with a Swifty way to do this...

I have an enum I want to execute code only for certain cases and do nothing all other times (non-exhaustive)

This seems overly verbose for Swift

if num == .One || num == .Two || num == .Three || num == .Four {
//code
}


This leaves me with a default case where I want to do nothing but still have to add something like a
print


switch num {
case .One, .Two. Three, .Four:
//code
default:
print("do nothing but I still need to put something here")
}


I would like to do something like this... does it exist in Swift?

if case .One, .Two, .Three, .Four == num {
//code
}

Answer
If [.One, .Two, .Three, .Four].contains(num) {
Comments