O'Reilly logo

Swift Functional Programming - Second Edition 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

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 example, we matched an optional by _?.

The wildcard pattern can be used to ignore data that we do not need and values that we do not want to match against. The following code example ...

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