LocalizedError, RecoverableError, CustomNSError

Swift 2 introduced error handling by way of the throws, do, try and catch keywords. It was designed to work hand-in-hand with Cocoa error handling conventions, such that any type conforming to the ErrorProtocol protocol (since renamed to Error) was implicitly bridged to NSError and Objective-C methods with an NSError** parameter, were imported by Swift as throwing methods.

nshipster.com/swift-foundation-error-protocols/

Swift’s error handling works well with Cocoa’s NSError system. Swift 3 added three little-known protocols to make this link even better:

In practice, make your errors conform to LocalizedError for clean user messages and, if needed, to CustomNSError for full Cocoa compatibility. RecoverableError is handy when you want to offer “Try again / Cancel” style options.

Using these protocols keeps error handling tidy, reusable, and fully compatible with Apple’s platforms.


Enjoyed this post?

Well, you could share the post with others, follow me with RSS Feeds, send me a comment via email, and/or leave a donation in the Tip Jar.


Tags

Category:

Tag:

Year: