Book Image

Learning Swift

By : Andrew J Wagner
Book Image

Learning Swift

By: Andrew J Wagner

Overview of this book

Table of Contents (18 chapters)
Learning Swift
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Debugging optionals


We already saw a couple of compiler errors that we commonly see because of optionals. If we try to call a method on an optional that we intended to call on the wrapped value, we will get an error. If we try to unwrap a value that is not actually optional, we will get an error that the variable or constant is not optional. We also need to be prepared for runtime errors that optionals can cause.

As discussed, optionals cause runtime errors if you try to forcefully unwrap an optional that is nil. This can happen with both explicit and implicit forced unwrapping. If you followed my advice so far in this chapter, this should be a rare occurrence. However, we all end up working with third-party code, and maybe they were lazy or maybe they used forced unwrapping to enforce their expectations about how their code should be used.

Also, we all suffer from laziness from time to time. It can be exhausting or discouraging to worry about all the edge cases when you are excited about...