O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, 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 an ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required