April 2017
Intermediate to advanced
316 pages
9h 33m
English
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 ...
Read now
Unlock full access