Wrap-up
Optionals are an invaluable component of Swift development. With their “possible value” semantics, they enable you to store and represent the results of lookup operations whose data may or may not exist. Optionals are a powerful workhorse construct that you regularly use in day-to-day operations.
Swift’s new early-return guard statement is a gift that eliminates awkwardness from your code. Now you can assign, unwrap, and use values with a clear path for missing values. Between guard and nil coalescing, Swift 2 can elegantly express both fail-on-nil and fallback-on-nil scenarios with a minimum of overhead and indenting.
Swift 2’s revised error handling has also started to eliminate the role of optionals as sentinel values. This usage continues to be common as it’s simple to use optionals to represent fail and success states for method calls. Ideally, optional sentinels will shrivel and die as the language matures and Apple’s APIs catch up with current language features. Until then, know that it’s always better to use throws with well-defined errors than to use optional-style sentinels in the code you control.
nil happens. Be prepared.