簡體   English   中英

Swift - 多個開關案例

[英]Swift - Multiple Switch Cases

我正在制作一個棋盤游戲,您必須在其中連接兩個對象。 每個對象都有一個類型,並且有 5 種不同的類型。 對於合並中的每種不同類型的組合,都會對游戲產生不同的影響。 現在,我正在為每個組合使用一個 switch 語句。 所以,我的代碼看起來像這樣。

struct Coin {
    var type = 1
}

// Each coin is of type Coin.   
switch(coin1.type, coin2.type) {
    case (1,1):
        functionNormal()
    case (1,2):
        functionUncommon()
    case (1,3):
        functionRare()
    ...
}

對象的位置不會改變結果。 (1,2) 合並將具有與 (2,1) 合並相同的效果。 有沒有更簡潔的方法來實現這一點?

您可以傳遞多個逗號分隔的案例:

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"
}

怎么樣 :

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.
}

如果您經常這樣做,您可以定義一個 minMax 函數來進一步減少每次所需的代碼量:

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()
  ...

請注意,您還可以將每個反轉對放在其 case 語句中:

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

[更新]

如果您有兩個以上的值要以“排列不敏感”的方式進行比較,您可以通過創建具有更多值的變體來擴展 minMax() 的想法:

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
   ...
}

}

您可以將對象放入一個集合(本質上是無序的)並檢查該集合是否包含元素的組合。 您有 15 (5! = 5+4+3+2+1) 個案例,因此您將有 15 個 if。

    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
        }
    }

object1.typeobject2.type分配給兩個臨時變量(我稱它們ab ),這樣a總是小於b 現在您只需要枚舉第一個數字小於第二個數字的情況。

struct Object {
    var type = 1
}

let (a, b) = object1.type < object2.type ? (object1.type, object2.type) : (object2.type, object1.type)
switch (a, b) {
case (1, 1): print("(1, 1)")
case (1, 2): print("(1, 2)")
case (1, 3): print("(1, 3)")
case (1, 4): print("(1, 4)")
case (1, 5): print("(1, 5)")
case (2, 2): print("(2, 2)")
case (2, 3): print("(2, 3)")
case (2, 4): print("(2, 4)")
case (2, 5): print("(2, 5)")
case (3, 3): print("(3, 3)")
case (3, 4): print("(3, 4)")
case (3, 5): print("(3, 5)")
case (4, 4): print("(4, 4)")
case (4, 5): print("(4, 5)")
case (5, 5): print("(5, 5)")

default: preconditionFailure("Values must each be in the range 1...5, with the first less than the second. Found: (\(a)\(b))")
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM