switch 구문으로 각각의 열거형 값을 일치시킬 수 있다:
directionToHead = .south
switch directionToHead {
case .north:
print("Lots of planets have a north")
case .south:
print("Watch out for penguins")
case .east:
print("Where the sun rises")
case .west:
print("Where the skies are blue")
}
// Prints "Watch out for penguins"
이 코드를 아래와 같이 읽을 수 있다:
"directionToHead 값을 고려합니다. .north 와 같은 케이스일 경우 "Lots of planets have a north" 를 출력합니다. .south 와 같은 케이스일 경우 "Watch out for penguins" 를 출력합니다."
제어 흐름 (Control Flow) 에서 설명 했듯이 switch 구문은 열거형 케이스를 고려할 때 완벽해야 한다. .west 에 대한 case 가 생략된다면 CompassPoint 케이스에 대해 모든 리스트가 고려되지 않았으므로 이 코드는 컴파일 되지 않는다. 완전성을 요구하면 열거형 케이스가 실수로 생략되지 않도록 해야 한다.
모든 열거형 케이스에 대해 case 를 제공하는 것이 적절하지 않은 경우 명시적으로 해결되지 않은 사례를 포함하는 default 케이스를 제공할 수 있다:
let somePlanet = Planet.earth
switch somePlanet {
case .earth:
print("Mostly harmless")
default:
print("Not a safe place for humans")
}
// Prints "Mostly harmless"