Swift version: 5.10
Optionals are a powerful source of safety in Swift, but can also be annoying if you find them littered throughout your code. Swift's nil coalescing operator helps you solve this problem by either unwrapping an optional if it has a value, or providing a default if the optional is empty.
Here's an example to get you started:
let name: String? = nil
let unwrappedName = name ?? "Anonymous"
Because name
is an optional string, we need to unwrap it safely to ensure it has a meaningful value. The nil coalescing operator – ??
– does exactly that, but if it finds the optional has no value then it uses a default instead. In this case, the default is "Anonymous". What this means is that unwrappedName
has the data type String
rather than String?
because it can be guaranteed to have a value.
You don't need to create a separate variable to use nil coalescing. For example, this works fine too:
print("Hello, \(name ?? "Anonymous")!")
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and more!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Available from iOS 7.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.