8 Optionals

Optionals are a special feature in Swift used to indicate that an instance may not have a value. When you see an optional, you know one of two things about that instance: either it has a value and it is ready for use, or it has no value. If an instance has no value associated with it, we say that it is nil.

You can use optionals with any type to signal that an instance is potentially nil. This feature distinguishes Swift from Objective-C, which only allows objects to be nil.

This chapter covers how to declare optional types, how to use optional binding to check whether an optional is nil and make use of its value if it has one, and how to use optional chaining to query a sequence of optional values.

Optional ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.