6. Errors

In Swift, as in any other programming language, things fail. In daily development tasks, you encounter both logical errors—that is, things that compile but don’t work the way you expect them to—and runtime errors—errors that arise from real-world conditions such as missing resources or inaccessible services. Swift 2’s redesigned error-handling system enables you to respond to both kinds of error conditions. Its response mechanisms range from assertions that fail fatally to error types that support recovery, enabling you to track what went wrong and offer runtime workarounds.

The updated error system takes into account Apple’s vast Cocoa/Cocoa Touch ecosystem. Cocoa has a particular way of working. APIs return a usable value or some ...

Get The Swift™ Developer’s Cookbook 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.