O'Reilly logo

Swift 3 Functional Programming by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Patterns and pattern matching

In the previous section, we looked at simple pattern matching examples for enumerations. In this section, we will examine patterns and pattern matching in detail.

The wildcard pattern

The wildcard pattern matches and ignores any value. It consists of an underscore, _. We use a wildcard pattern when we do not care about the values being matched against.

For instance, the following code example ignores the matched values:

for _ in 1...5 {
    print("The value in range is ignored")
}

We use _ to ignore the value in the iteration.

The wildcard pattern can be used with optionals as follows:

let anOptionalString: String? = nil

switch anOptionalString {
    case _?: print ("Some")
    case nil: print ("None")
}

As seen from the preceding ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required