April 2026
Intermediate
683 pages
25h 29m
English
Although it’s dangerous, it is an occasional necessity to implicitly unwrap an optional. Using this technique, you can tell Swift that it can assume an optional to contain a value and behave accordingly, risking a runtime crash if accessed when nil.
Let’s start discovering this feature with the regular case in Listing 6.14. Here you have the optional name variable. Somewhere along the way, you assign the value "Alice" to it—but because the variable is optional, it might have been nil too.
var name: String?// ...name = "Alice"// ...print(name ?? "")
Listing 6.14 Accessing Optional with Fallback Value
When you access name in the last line, you have to provide a fallback value with ?? in case name is ...
Read now
Unlock full access