Implicitly Unwrapped Optionals

At this point it is worth mentioning implicitly unwrapped optionals, though you will not make much use of them until we discuss classes and class initialization later. Implicitly unwrapped optionals are like regular optional types, but with one important difference: you do not need to unwrap them. How is that the case? It has to do with how you declare them. Take a look at the code below, which refactors the example above to work with an implicitly unwrapped optional.

import Cocoa

var errorCodeString: String!
errorCodeString = "404"
print(errorCodeString)

Here, the optional is declared with the !, which signifies that it is an implicitly unwrapped optional. The conditional is removed because using ...

Get Swift Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.