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 optional pattern

An optional pattern matches values wrapped in a Some(Wrapped)case of an Optional<Wrapped> or ImplicitlyUnwrappedOptional<Wrapped> enumeration. Optional patterns consist of an identifier pattern followed immediately by a question mark and appear in the same places as enumeration case patterns. The following example presents optional pattern matching:

let anOptionalString: String? = nil switch anOptionalString { case let something?: print("\(something)") case nil: print ("None") } 

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