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

Optional binding

The better approach would be to use the optional binding technique to find out whether an optional contains a value or not. If it contains a value, we will be able to unwrap it and put it into a temporary constant or variable.

The following example presents optional binding:

let nilName: String? = nil if let familyName = nilName {     let greetingfamilyName = "Hello, Mr. \(familyName)" } else {     // Optional does not have a value } 

The if let familyName = nilName statement will assign the optional value to a new variable named familyName. The right-hand side of the assignment has to be an optional, otherwise, the compiler will issue an error. Also, this approach ensures that we are using the unwrapped temporary version so it ...

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