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