2017-06-20 5 views
-1

私は列挙型の配列を持っていますが、これらのケースでswitch文を実行しますが、このエラーが発生します: '列挙型' North [列挙型]の型が見つかりません。列挙型の配列を持つswitch文

enum Directions { 
    case north 
    case west 
    case east 
    case south 

    static let all = [north, west, east, south] 
} 

class MyViewController { 
    var directions = Directions.all 

    func foo() { 
     switch directions { 
     case .north: // Error here ('Enum case 'North' not found in type '[Directions]') 
      print("Going north") 
     } 
    } 
} 
+2

なぜswitch文に列挙型の配列を使用していますか? –

+5

あなたがしようとしていることは無意味です。アレイ全体ではなく、アレイ内の各項目のスイッチを実行する必要があります。例えば、あなたはこの質問を[north、west、east、south]と等しい北にすることができますか? –

+0

この質問は、列挙型の配列を使用して、それを使用しているものの詳細です。私は自分の関数の特定の値をチェックします。したがって、ex: let goingInDirection = .north let foo(goingInDirection){ switch ... } – andromedainiative

答えて

2

あなたはスイッチ配列をループにして、あなたが使用できる最初の必要性

func foo() { 
    for direction in directions { 
     switch direction { 
     case .north: print("Going north") 
     case .west: print("Going west") 
     case .east: print("Going east") 
     case .south: print("Going south") 
     } 
    } 
} 

The name of the enum should be singular so Direction instead of Directions

-3

あなたは、以下の方法

enum Directions { 
    case north 
    case west 
    case east 
    case south 
} 

class MyViewController { 

    func foo() { 
     switch Directions 
     { 
     case Directions.north: 
      print("Going north") 
     } 
    } 
} 
0

問題に言及コードを使用することができます方向の配列を方向の列挙の場合と比較していることです。以下の配列の特定の要素を比較する必要があります。

enum Directions { 

    case north 
    case south 
    case east 
    case west 
    static let all = [north, west, east, south] 
} 


class MyViewController { 
    var dir = Directions.all 

    func testing(){ 

     switch dir[0] { 
      case .north: 
       print("north") 
      default: 
       print("default") 
     } 
    } 
} 

var a = MyViewController() 

a.testing() 

// out put : north 
関連する問題