Advanced Pattern Matching

Identifier Pattern

released Fri, 15 Feb 2019
Swift Version 5.0

Identifier Pattern

Matches a concrete value. This is how things work in Objective-C's switch implementation:

Here, we have a special case just for the number 5

let number = 4
switch number {
case 5: print("it is a 5")
default: print("it is something else")

We can also match strings, see our code below to greet people in their native language.

let language = "Japanese"
switch name {
case "Japanese": print("")
case "English": print("Hello!")
case "German": print("Guten Tag")