Book Image

Mastering Swift

By : Jon Hoffman
Book Image

Mastering Swift

By: Jon Hoffman

Overview of this book

Table of Contents (22 chapters)
Mastering Swift
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Chapter 8. Using Optional Type and Optional Chaining

When I first started using Swift, the concept that I had the most trouble with was optionals. Coming from an Objective-C, C, Java, and Python background, I was able to relate to most of Swift's features to how things worked in one of the other languages that I knew, but optionals were different. There really was nothing like optionals in the other languages that I used, so it took a lot of reading to fully understand them. While I briefly covered optionals in Chapter 2, Learning about Variables, Constants, Strings, and Operators, which gave enough of an overview to get started with, we need to cover a lot more information to really understand what optionals are, how to properly use them, and why they are so important in the Swift language.

In this chapter, we will cover the following topics:

  • An introduction to optional types

  • The need for optional types in Swift

  • Unwrapping and optional

  • Optional binding

  • Optional chaining