6.9 Nil Coalescing Operator
The nil coalescing operator (??) allows a default value to be used in the event that an optional has a nil value. The following example will output text which reads “Welcome back, customer” because the customerName optional is set to nil:
let customerName: String? = nil
print("Welcome back, \(customerName ?? "customer")")
If, on the other hand, customerName is not nil, the optional will be unwrapped and the assigned value displayed:
let customerName: String? = "John"
print("Welcome back, \(customerName ?? "customer")")
On execution, the print statement output will now read “Welcome back, John”.