Swift - Multiple Switch Cases

how about :

struct Object{
    var type = 1
}

let lowType  = min(object1.type,object2.type)
let highType = max(object1.type,object2.type)
switch( lowType, highType )
{
  case (1,1):
      doSome()
  case (1,2):
      doSome2()
  case (1,3):
    doSome3()
  ...
  // you will only need to specify the combinations where the first
  // type is less or equal to the other.
}

If you will be doing this often, you could define a minMax function to further reduce the amount of code needed each time:

func minMax<T:Comparable>(_ a:T, _ b:T) ->(T,T) 
{ return a<b ? (a,b) : (b,a) }

switch minMax(object1.type,object2.type)
{
  case (1,1):
      doSome()
  case (1,2):
      doSome2()
  ...

note that you can also put each inverted pair in its case statement:

switch(object1.type,object2.type)
{
  case (1,1):
      doSome()
  case (1,2),(2,1):
      doSome2()
  case (1,3),(3,1):
      doSome3()
  ...
}

[UPDATE]

If you have more than two values to compare in a "permutation insensitive" fashion, you could expand on the minMax() idea by creating variants with more values:

func minToMax<T:Comparable>(_ a:T, _ b:T) -> (T,T) 
{ return a<b ? (a,b) : (b,a) }

func minToMax<T:Comparable>(_ a:T, _ b:T, _ c:T) -> (T,T,T) 
{ return { ($0[0],$0[1],$0[2]) }([a,b,c].sorted()) }

func minToMax<T:Comparable>(_ a:T, _ b:T, _ c:T, _ d:T) -> (T,T,T,T) 
{ return { ($0[0],$0[1],$0[2],$0[3]) }([a,b,c,d].sorted()) }

switch minToMax(object1.type,object2.type,object3.type)
{
   case (1,2,3)      : doSome1() // permutations of 1,2,3
   case (1,2,4)      : doSome2() // permutations of 1,2,4
   case (1,2,_)      : doSome3() // permutations of 1,2,anything else
   case (1,5...6,10) : doSome4() // more elaborate permutations
   ...
}

}


You can pass multiple comma-separated cases:

switch switchValue {
case .none:
    return "none"
case .one, .two:
    return "multiple values from case 2"
case .three, .four, .five:
    return "Multiple values from case 3"
}

You can throw the objects into a set (which is inherently unordered) and check that the set contains a combination of elements. You have 15 (5! = 5+4+3+2+1) cases so you will have 15 ifs.

    enum PrimaryColor {
        case red, yellow, blue
    }
    enum SecondaryColor {
        case orange, green, purple
        init?(firstColor: PrimaryColor, secondColor: PrimaryColor) {
            let colors = Set<PrimaryColor>([firstColor, secondColor])
            if colors.contains(.red)  && colors.contains(.blue) {
                self = .purple
                return
            }
            if colors.contains(.yellow)  && colors.contains(.blue) {
                self = .green
                return
            }
            if colors.contains(.red)  && colors.contains(.yellow) {
                self = .orange
                return
            }
            //if you care about the diagonal check firstColor == secondColor  && colors.contains(.red) etc.
            return nil
        }
    }