Dribbler Dribbler - 24 days ago 7
Swift Question

Same result different case in Swift?

I'd like in a switch statement the same thing to be done for different cases, e.g.

let a = 2
switch a {
case 2:
print( "hi" )
case 3:
print( "hi" )
default:
print( "monkey" )
}


I've tried this syntax, but it errors:

case 2 || 3: ...


Is there a syntactical way to do this rather than to put what's in the duplicate cases into a refactored function?

Thanks in advance!

Answer Source

Thanks for the quick responses--the answer was to insert a comma or to use a closed range such as:

let a = 2
switch a {
case 2, 3:
    print( "hi" )
default:
    print( "monkey" )
}

or

let a = 2
switch a {
case 2...3:
    print( "hi" )
default:
    print( "monkey" )
}