<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Obviously, I’m in favor of this one. +1!</div><div class=""><br class=""></div><div class="">I think I did prefer the older name of CustomUserInfoError for the domain/code/userInfo protocol, rather than CustomNSError. This is just because I’d like to be able to do a global search through my project for “NSError” and have it turn up empty. Maybe a silly reason, I know. ;-)</div><div class=""><br class=""></div><div class="">I sent a few more comments off-list before I realized you’d posted the proposal already, so you can peruse, consider, and/or discard those at your leisure. It’s nothing terribly important, though; this proposal is pretty great as is.</div><div class=""><br class=""></div><div class="">Thanks so much for doing this! I’m really excited for better error handling in Swift 3.</div><div class=""><br class=""></div><div class="">Charles</div><br class=""><div><blockquote type="cite" class=""><div class="">On Jun 27, 2016, at 1:17 PM, Douglas Gregor <<a href="mailto:dgregor@apple.com" class="">dgregor@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi all,<div class=""><br class=""></div><div class="">Proposal link: <a href="https://github.com/DougGregor/swift-evolution/blob/nserror-bridging/proposals/0000-nserror-bridging.md" class="">https://github.com/DougGregor/swift-evolution/blob/nserror-bridging/proposals/0000-nserror-bridging.md</a></div><div class=""><br class=""></div><div class="">Here is a detailed proposal draft for bridging NSError to ErrorProtocol. Getting this right is surprisingly involved, so the detailed design on this proposal is fairly large. Comments welcome!</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>- Doug</div><div class=""><br class=""></div><div class=""><h1 style="box-sizing: border-box; font-size: 2.25em; margin-right: 0px; margin-bottom: 16px; margin-left: 0px; line-height: 1.2; padding-bottom: 0.3em; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(238, 238, 238); color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; background-color: rgb(255, 255, 255); margin-top: 0px !important;" class="">NSError Bridging</h1><ul style="box-sizing: border-box; padding-left: 2em; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class=""><li style="box-sizing: border-box;" class="">Proposal: <a href="https://github.com/DougGregor/swift-evolution/blob/nserror-bridging/proposals/NNNN-nserror-bridging.md" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none;" class="">SE-NNNN</a></li><li style="box-sizing: border-box;" class="">Author: <a href="https://github.com/DougGregor" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none;" class="">Doug Gregor</a>, <a href="https://github.com/CharlesJS" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none;" class="">Charles Srstka</a></li><li style="box-sizing: border-box;" class="">Status: <span style="box-sizing: border-box;" class="">Awaiting review</span></li><li style="box-sizing: border-box;" class="">Review manager: TBD</li></ul><h2 style="box-sizing: border-box; margin-top: 1em; margin-bottom: 16px; line-height: 1.225; font-size: 1.75em; padding-bottom: 0.3em; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(238, 238, 238); color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; background-color: rgb(255, 255, 255);" class=""><a id="user-content-introduction" class="anchor" href="https://github.com/DougGregor/swift-evolution/blob/nserror-bridging/proposals/0000-nserror-bridging.md#introduction" aria-hidden="true" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none; display: inline-block; padding-right: 2px; margin-left: -18px; line-height: 1;"><svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Introduction</h2><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">Swift's error handling model interoperates directly with Cocoa's NSError conventions. For example, an Objective-C method with an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError**</code> parameter, e.g.,</p><div class="highlight highlight-source-objc" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class="">- (nullable <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">NSURL</span> *)replaceItemAtURL:(<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">NSURL</span> *)url options:(<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">NSFileVersionReplacingOptions</span>)options error:(<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">NSError</span> **)error;</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">will be imported as a throwing method:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">replaceItem</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">at</span> <span class="pl-smi" style="box-sizing: border-box;">url</span>: URL, options: ReplacingOptions <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> []) throws <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> URL</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">Swift bridges between <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">ErrorProtocol</code>-conforming types and <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> so, for example, a Swift <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">enum</code> that conforms to<code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">ErrorProtocol</code> can be thrown and will be reflected as an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> with a suitable domain and code. Moreover, an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code>produced with that domain and code can be caught as the Swift <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">enum</code> type, providing round-tripping so that Swift can deal in <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">ErrorProtocol</code> values while Objective-C deals in <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> objects.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">However, the interoperability is incomplete in a number of ways, which results in Swift programs having to walk a careful line between the <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">ErrorProtocol</code>-based Swift way and the <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code>-based way. This proposal attempts to bridge those gaps.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">Swift-evolution thread: <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160502/016618.html" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none;" class="">Charles Srstka's pitch for Consistent bridging for NSErrors at the language boundary</a>, which discussed Charles' <a href="https://github.com/apple/swift-evolution/pull/331" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none;" class="">original proposal</a> that addressed these issues by providing <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> to <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">ErrorProtocol</code> bridging and exposing the domain, code, and user-info dictionary for all errors. This proposal expands upon that work, but without directly exposing the domain, code, and user-info.</p><h2 style="box-sizing: border-box; margin-top: 1em; margin-bottom: 16px; line-height: 1.225; font-size: 1.75em; padding-bottom: 0.3em; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(238, 238, 238); color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; background-color: rgb(255, 255, 255);" class=""><a id="user-content-motivation" class="anchor" href="https://github.com/DougGregor/swift-evolution/blob/nserror-bridging/proposals/0000-nserror-bridging.md#motivation" aria-hidden="true" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none; display: inline-block; padding-right: 2px; margin-left: -18px; line-height: 1;"><svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Motivation</h2><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">There are a number of weaknesses in Swift's interoperability with Cocoa's error model, including:</p><ol style="box-sizing: border-box; padding-left: 2em; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class=""><li style="box-sizing: border-box;" class=""><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class="">There is no good way to provide important error information when throwing an error from Swift. For example, let's consider a simple application-defined error in Swift:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px;"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">enum</span> HomeworkError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span>, ErrorProtocol {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> forgotten
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> lost
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> dogAteIt
}</pre></div><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class="">One can throw <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">HomeworkError.dogAteIt</code> and it can be interpreted as an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> by Objective-C with an appropriate error domain (effectively, the mangled name of the <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">HomeworkError</code> type) and code (effectively, the case discriminator). However, one cannot provide a localized description, help anchor, recovery attempter, or any other information commonly placed into the <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">userInfo</code> dictionary of an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code>. To provide these values, one must specifically construct an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> in Swift, e.g.,</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px;"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">throw</span> NSError(code: HomeworkError<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>dogAteIt<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">rawValue</span>,
domain: HomeworkError<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>_domain,
userInfo: [ NSLocalizedDescriptionKey <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> <span class="pl-s" style="box-sizing: border-box; color: rgb(24, 54, 145);"><span class="pl-pds" style="box-sizing: border-box;">"</span>the dog ate it<span class="pl-pds" style="box-sizing: border-box;">"</span></span> ])</pre></div></li><li style="box-sizing: border-box;" class=""><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class="">There is no good way to get information typically associated with <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code>'s <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">userInfo</code> in Swift. For example, the Swift-natural way to catch a specific error in the <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">AVError</code> error domain doesn't give one access to the <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">userInfo</code> dictionary, e.g.,:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px;"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">catch</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as</span> AVError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">where</span> error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">==</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>diskFull {
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// AVError is an enum, so one only gets the equivalent of the code.</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// There is no way to access the localized description (for example) or</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// any other information typically stored in the ``userInfo`` dictionary.</span>
}</pre></div><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class="">The workaround is to catch as an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code>, which is quite a bit more ugly:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px;"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">catch</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as</span> NSError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">where</span> error<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>_domain <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">==</span> AVFoundationErrorDomain <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">&&</span> error<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>_code <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">==</span> AVFoundationErrorDomain<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>diskFull<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">rawValue</span> {
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// okay: userInfo is finally accessible, but still weakly typed</span>
}</pre></div><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class="">This makes it extremely hard to access common information, such as the localized description. Moreover, the <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">userInfo</code> dictionary is effectively untyped so, for example, one has to know a priori that the value associated with the known <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">AVErrorDeviceKey</code> will be typed as <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">CMTime</code>:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px;"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">catch</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as</span> NSError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">where</span> error<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>_domain <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> AVFoundationErrorDomain {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">if</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> time <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> error<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>userInfo[AVErrorDeviceKey] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as?</span> CMTime {
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// ...</span>
}
}</pre></div><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class="">It would be far better if one could catch an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">AVError</code> directly and query the time in a type-safe manner:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px;"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">catch</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as</span> AVError {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">if</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> time <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> error<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>time {
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// ...</span>
}
}</pre></div></li><li style="box-sizing: border-box;" class=""><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class=""><code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> is inconsistently bridged with <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">ErrorProtocol</code>. Swift interoperates by translating between <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> and <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">ErrorProtocol</code> when mapping between a throwing Swift method/initializer and an Objective-C method with an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError**</code> parameter. However, an Objective-C method that takes an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError*</code> parameter (e.g., to render it) does not bridge to <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">ErrorProtocol</code>, meaning that <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> is part of the API in Swift in some places (but not others). For example, <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> leaks through when the following <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">UIDocument</code> API in Objective-C:</p><div class="highlight highlight-source-objc" style="box-sizing: border-box; margin-bottom: 16px;"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class="">- (<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">void</span>)handleError:(<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">NSError</span> *)error userInteractionPermitted:(<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">BOOL</span>)userInteractionPermitted;</pre></div><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class="">is imported into Swift as follows:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px;"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">handleError</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">_</span> <span class="pl-smi" style="box-sizing: border-box;">error</span>: NSError, userInteractionPermitted: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Bool</span>)</pre></div><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class="">One would expect the first parameter to be imported as <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">ErrorProtocol</code>. </p></li></ol><h2 style="box-sizing: border-box; margin-top: 1em; margin-bottom: 16px; line-height: 1.225; font-size: 1.75em; padding-bottom: 0.3em; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(238, 238, 238); color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; background-color: rgb(255, 255, 255);" class=""><a id="user-content-proposed-solution" class="anchor" href="https://github.com/DougGregor/swift-evolution/blob/nserror-bridging/proposals/0000-nserror-bridging.md#proposed-solution" aria-hidden="true" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none; display: inline-block; padding-right: 2px; margin-left: -18px; line-height: 1;"><svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Proposed solution</h2><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">This proposal involves directly addressing (1)-(3) with new protocols and a different way of bridging Objective-C error code types into Swift, along with some conveniences for working with Cocoa errors:</p><ol style="box-sizing: border-box; padding-left: 2em; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class=""><li style="box-sizing: border-box;" class=""><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class="">Introduce three new protocols for describing more information about errors: <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">LocalizedError</code>, <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">RecoverableError</code>, and<code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">CustomNSError</code>. For example, an error type can provide a localized description by conforming to <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">LocalizedError</code>:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px;"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">extension</span> HomeworkError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> LocalizedError {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> errorDescription: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>? {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">switch</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">self</span> {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>forgotten: <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> NSLocalizedString(<span class="pl-s" style="box-sizing: border-box; color: rgb(24, 54, 145);"><span class="pl-pds" style="box-sizing: border-box;">"</span>I forgot it<span class="pl-pds" style="box-sizing: border-box;">"</span></span>)
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>lost: <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> NSLocalizedString(<span class="pl-s" style="box-sizing: border-box; color: rgb(24, 54, 145);"><span class="pl-pds" style="box-sizing: border-box;">"</span>I lost it<span class="pl-pds" style="box-sizing: border-box;">"</span></span>)
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>dogAteIt: <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> NSLocalizedString(<span class="pl-s" style="box-sizing: border-box; color: rgb(24, 54, 145);"><span class="pl-pds" style="box-sizing: border-box;">"</span>The dog ate it<span class="pl-pds" style="box-sizing: border-box;">"</span></span>)
}
}
}</pre></div></li><li style="box-sizing: border-box;" class=""><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class="">Imported Objective-C error types should be mapped to struct types that store an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> so that no information is lost when bridging from an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> to the Swift error types. We propose to introduce a new macro, <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NS_ERROR_ENUM</code>, that one can use to both declare an enumeration type used to describe the error codes as well as tying that type to a specific domain constant, e.g.,</p><div class="highlight highlight-source-objc" style="box-sizing: border-box; margin-bottom: 16px;"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">typedef</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">NS_ERROR_ENUM</span>(<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">NSInteger</span>, AVError, AVFoundationErrorDomain) {
AVErrorUnknown = -<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">11800</span>,
AVErrorOutOfMemory = -<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">11801</span>,
AVErrorSessionNotRunning = -<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">11803</span>,
AVErrorDeviceAlreadyUsedByAnotherSession = -<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">11804</span>,
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// ...</span>
}</pre></div><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class="">The imported <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">AVError</code> will have a struct that allows one to access the <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">userInfo</code> dictionary directly. This retains the ability to catch via a specific code, e.g.,</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px;"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">catch</span> AVError<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>outOfMemory {
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// ...</span>
}</pre></div><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class="">However, catching a specific error as a value doesn't lose information:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px;"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">catch</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as</span> AVError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">where</span> error<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>code <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">==</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>sessionNotRunning {
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// able to access userInfo here!</span>
}</pre></div><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class="">This also gives the ability for one to add typed accessors for known keys within the <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">userInfo</code> dictionary:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px;"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">extension</span> AVError {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> time: CMTime? {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">get</span> {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> userInfo[AVErrorTimeKey] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as?</span> CMTime?
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">set</span> {
userInfo[AVErrorTimeKey] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> newValue<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">map</span> { $<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">0</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as</span> CMTime }
}
}
}</pre></div></li><li style="box-sizing: border-box;" class=""><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class="">Bridge <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> to <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">ErrorProtocol</code>, so that all <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> uses are bridged consistently. For example, this means that the Objective-C API:</p><div class="highlight highlight-source-objc" style="box-sizing: border-box; margin-bottom: 16px;"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class="">- (<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">void</span>)handleError:(<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">NSError</span> *)error userInteractionPermitted:(<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">BOOL</span>)userInteractionPermitted;</pre></div><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class="">is imported into Swift as:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px;"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">handleError</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">_</span> <span class="pl-smi" style="box-sizing: border-box;">error</span>: ErrorProtocol, userInteractionPermitted: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Bool</span>)</pre></div><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class="">This will use the same bridging logic in the Clang importer that we use for other value types (<code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">Array</code>, <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">String</code>, <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">URL</code>, etc.), but with the runtime translation we've already been doing for catching/throwing errors.</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class="">When we introduce this bridging, we will need to remove <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code>'s conformance to <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">ErrorProtocol</code> to avoid creating cyclic implicit conversions. However, we still need an easy way to create an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">ErrorProtocol</code> instance from an arbitrary <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code>, e.g.,</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px;"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">extension</span> NSError {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> asError: ErrorProtocol { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">...</span> }
}</pre></div></li><li style="box-sizing: border-box;" class=""><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class="">In Foundation, add an extension to <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">ErrorProtocol</code> that provides typed access to the common user-info keys. Note that we focus only on those user-info keys that are read by user code (vs. only accessed by frameworks):</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px;"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">extension</span> ErrorProtocol {
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// Note: for exposition only. Not actual API.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">private</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> userInfo: [NSObject <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">AnyObject</span>] {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> (<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">self</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as!</span> NSError)<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>userInfo
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> localizedDescription: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span> {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> (<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">self</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as!</span> NSError)<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>localizedDescription
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> filePath: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>? {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> userInfo[NSFilePathErrorKey] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as?</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> stringEncoding: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>Encoding? {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> (userInfo[NSStringEncodingErrorKey] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as?</span> NSNumber)
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">map</span> { <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>Encoding(rawValue: $<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">0</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">uintValue</span>) }
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> underlying: ErrorProtocol? {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> (userInfo[NSUnderlyingErrorKey] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as?</span> NSError)?<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>asError
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> url: URL? {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> userInfo[NSURLErrorKey] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as?</span> URL
}
}</pre></div></li><li style="box-sizing: border-box;" class=""><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;" class="">Rename <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">ErrorProtocol</code> to <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">Error</code>: once we've completed the bridging story, <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">Error</code> becomes the primary way to work with error types in Swift, and the value type to which <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> is bridged:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px;"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">handleError</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">_</span> <span class="pl-smi" style="box-sizing: border-box;">error</span>: Error, userInteractionPermitted: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Bool</span>)</pre></div></li></ol><h2 style="box-sizing: border-box; margin-top: 1em; margin-bottom: 16px; line-height: 1.225; font-size: 1.75em; padding-bottom: 0.3em; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(238, 238, 238); color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; background-color: rgb(255, 255, 255);" class=""><a id="user-content-detailed-design" class="anchor" href="https://github.com/DougGregor/swift-evolution/blob/nserror-bridging/proposals/0000-nserror-bridging.md#detailed-design" aria-hidden="true" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none; display: inline-block; padding-right: 2px; margin-left: -18px; line-height: 1;"><svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Detailed design</h2><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">This section details both the design (including the various new protocols, mapping from Objective-C error code enumeration types into Swift types, etc.) and the efficient implementation of this design to interoperate with <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code>. Throughout the detailed design, we already assume the name change from <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">ErrorProtocol</code> to <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">Error</code>.</p><h3 style="box-sizing: border-box; margin-top: 1em; margin-bottom: 16px; line-height: 1.43; font-size: 1.5em; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; background-color: rgb(255, 255, 255);" class=""><a id="user-content-new-protocols" class="anchor" href="https://github.com/DougGregor/swift-evolution/blob/nserror-bridging/proposals/0000-nserror-bridging.md#new-protocols" aria-hidden="true" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none; display: inline-block; padding-right: 2px; margin-left: -18px; line-height: 1.2;"><svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>New protocols</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">This proposal introduces several new protocols that allow error types to expose more information about error types.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">The <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">LocalizedError</code> protocol describes an error that provides localized messages for display to the end user, all of which provide default implementations. The conforming type can provide implementations for any subset of these requirements:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">protocol</span> LocalizedError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> Error {
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// A localized message describing what error occurred.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> errorDescription: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>? { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">get</span> }
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// A localized message describing the reason for the failure.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> failureReason: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>? { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">get</span> }
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// A localized message describing how one might recover from the failure.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> recoverySuggestion: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>? { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">get</span> }
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// A localized message providing "help" text if the user requests help.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> helpAnchor: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>? { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">get</span> }
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">extension</span> LocalizedError {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> errorDescription: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>? { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">nil</span> }
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> failureReason: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>? { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">nil</span> }
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> recoverySuggestion: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>? { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">nil</span> }
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> helpAnchor: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>? { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">nil</span> }
}</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">The <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">RecoverableError</code> protocol describes an error that might be recoverable: </p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">protocol</span> RecoverableError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> Error {
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// Provides a set of possible recovery options to present to the user.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> recoveryOptions: [<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>] { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">get</span> }
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// Attempt to recover from this error when the user selected the</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// option at the given index. This routine must call resultHandler and</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// indicate whether recovery was successful (or not).</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">///</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// This entry point is used for recovery of errors handled at a</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// "document" granularity, that do not affect the entire</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// application.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">attemptRecovery</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">optionIndex</span> <span class="pl-smi" style="box-sizing: border-box;">recoveryOptionIndex</span>: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span>,
<span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">andThen</span> <span class="pl-smi" style="box-sizing: border-box;">resultHandler</span>: (recovered: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Bool</span>) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Void</span>)
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// Attempt to recover from this error when the user selected the</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// option at the given index. Returns true to indicate</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// successful recovery, and false otherwise.</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">///</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// This entry point is used for recovery of errors handled at</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// the "application" granularity, where nothing else in the</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// application can proceed until the attmpted error recovery</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// completes.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">attemptRecovery</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">optionIndex</span> <span class="pl-smi" style="box-sizing: border-box;">recoveryOptionIndex</span>: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span>) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Bool</span>
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">extension</span> RecoverableError {
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// By default, implements document-modal recovery via application-model</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// recovery.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">attemptRecovery</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">optionIndex</span> <span class="pl-smi" style="box-sizing: border-box;">recoveryOptionIndex</span>: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span>,
<span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">andThen</span> <span class="pl-smi" style="box-sizing: border-box;">resultHandler</span>: (<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Bool</span>) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Void</span>) {
resultHandler(recovered: attemptRecovery(optionIndex: recoveryOptionIndex))
}
}</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">Error types that conform to <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">RecoverableError</code> may be given an opportunity to recover from the error. The user can be presented with some number of (localized) recovery options, described by <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">recoveryOptions</code>, and the selected option will be passed to the appropriate <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">attemptRecovery</code> method.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">The <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">CustomNSError</code> protocol describes an error that wants to provide custom <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> information. This can be used, e.g., to provide a specific domain/code or to populate <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code>'s <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">userInfo</code> dictionary with values for custom keys that can be accessed from Objective-C code but are not covered by the other protocols.</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// Describes an error type that fills in the userInfo directly.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">protocol</span> CustomNSError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> Error {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> errorDomain: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span> { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">get</span> }
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> errorCode: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span> { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">get</span> }
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> errorUserInfo: [<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">AnyObject</span>] { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">get</span> }
}</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">Note that, unlike with <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code>, the provided <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">errorUserInfo</code> requires <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">String</code> keys. This is in line with common practice for <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> and is important for the implementation (see below). All of these properties are defaulted, so one can provide any subset:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">extension</span> CustomNSError {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> errorDomain: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span> { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">...</span> }
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> errorCode: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span> { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">...</span> }
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> errorUserInfo: [<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">AnyObject</span>] { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">...</span> }
}</pre></div><h3 style="box-sizing: border-box; margin-top: 1em; margin-bottom: 16px; line-height: 1.43; font-size: 1.5em; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; background-color: rgb(255, 255, 255);" class=""><a id="user-content-mapping-error-types-to-nserror" class="anchor" href="https://github.com/DougGregor/swift-evolution/blob/nserror-bridging/proposals/0000-nserror-bridging.md#mapping-error-types-to-nserror" aria-hidden="true" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none; display: inline-block; padding-right: 2px; margin-left: -18px; line-height: 1.2;"><svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Mapping error types to <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: inherit; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code></h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">Every type that conforms to the <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">Error</code> protocol is implicitly bridged to <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code>. This has been the case since Swift 2, where the compiler provides a domain (i.e., the mangled name of the type) and code (based on the discriminator of the enumeration type). This proposal also allows for the <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">userInfo</code> dictionary to be populated by the runtime, which will check for conformance to the various protocols (<code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">LocalizedError</code>, <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">RecoverableError</code>, or <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">CustomNSError</code>) to retrieve information.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">Conceptually, this could be implemented by eagerly creating a <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">userInfo</code> dictionary for a given instance of <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">Error</code>:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">createUserInfo</span>(error: Error) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> [NSObject : <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">AnyObject</span>] {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> userInfo: [NSObject <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">AnyObject</span>] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> [<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span>]
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// Retrieve custom userInfo information.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">if</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> customUserInfoError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as?</span> CustomNSError {
userInfo <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> customUserInfoError<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>userInfo
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">if</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> localizedError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as?</span> LocalizedError {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">if</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> description <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> localizedError<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>errorDescription {
userInfo[NSLocalizedDescriptionKey] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> description
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">if</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> reason <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> localizedError<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>failureReason {
userInfo[NSLocalizedFailureReasonErrorKey] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> reason
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">if</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> suggestion <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> localizedError<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>recoverySuggestion {
userInfo[NSLocalizedRecoverySuggestionErrorKey] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> suggestion
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">if</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> helpAnchor <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> localizedError<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>helpAnchor {
userInfo[NSHelpAnchorErrorKey] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> helpAnchor
}
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">if</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> recoverableError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as?</span> RecoverableError {
userInfo[NSLocalizedRecoveryOptionsErrorKey] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> recoverableError<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>recoveryOptions
userInfo[NSRecoveryAttempterErrorKey] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> RecoveryAttempter()
}
}</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">The <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">RecoveryAttempter</code> class is an implementation detail. It will implement the informal protocol <a href="https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Protocols/NSErrorRecoveryAttempting_Protocol/" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none;" class=""><code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSErrorRecoveryAttempting</code></a> for the given error:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">class</span> RecoveryAttempter <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> NSObject {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">@objc</span>(attemptRecoveryFromError:optionIndex:delegate:didRecoverSelector:contextInfo:)
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">attemptRecovery</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">fromError</span> <span class="pl-smi" style="box-sizing: border-box;">nsError</span>: NSError,
<span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">optionIndex</span> <span class="pl-smi" style="box-sizing: border-box;">recoveryOptionIndex</span>: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span>,
delegate: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">AnyObject</span>?,
didRecoverSelector: Selector,
contextInfo: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">UnsafeMutablePointer</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);"><</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Void</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">></span>) {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> nsError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as!</span> RecoverableError
error<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>attemptRecovery(optionIndex: recoveryOptionIndex) { success <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">in</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// Exposition only: this part will actually have to be</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// implemented in Objective-C to pass the BOOL and void* through.</span>
delegate?<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>perform(didRecoverSelector, with: success, with: contextInfo)
}
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">@objc</span>(attemptRecoveryFromError:optionIndex:)
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">attemptRecovery</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">fromError</span> <span class="pl-smi" style="box-sizing: border-box;">nsError</span>: NSError,
<span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">optionIndex</span> <span class="pl-smi" style="box-sizing: border-box;">recoveryOptionIndex</span>: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span>) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Bool</span> {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> nsError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as!</span> RecoverableError
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> error<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>attemptRecovery(optionIndex: recoveryOptionIndex)
}
}</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">The actual the population of the <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">userInfo</code> dictionary should not be eager. <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> provides the notion of global "user info value providers" that it uses to lazily request the values for certain keys, via <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">setUserInfoValueProvider(forDomain:provider:)</code>, which is declared as:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">extension</span> NSError {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">@available</span>(OSX <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">10</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">11</span>, iOS <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">9</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">0</span>, tvOS <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">9</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">0</span>, watchOS <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">2</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">0</span>, <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">*</span>)
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">class</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">setUserInfoValueProvider</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">forDomain</span> <span class="pl-smi" style="box-sizing: border-box;">errorDomain</span>: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>,
provider: ((NSError, <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">AnyObject</span>?)? = nil)
}</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">The runtime would need to register a user info value provider for each error type the first time it is bridged into <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code>, supplying the domain and the following user info value provider function:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">userInfoValueProvider</span>(nsError: NSError, key: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">AnyObject</span>? {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> nsError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as!</span> Error
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">switch</span> key {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> NSLocalizedDescriptionKey:
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> (error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as?</span> LocalizedError)?<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>errorDescription
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> NSLocalizedFailureReasonErrorKey:
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> (error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as?</span> LocalizedError)?<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>failureReason
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> NSLocalizedRecoverySuggestionErrorKey:
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> (error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as?</span> LocalizedError)?<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>recoverySuggestion
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> NSHelpAnchorErrorKey:
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> (error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as?</span> LocalizedError)?<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>helpAnchor
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> NSLocalizedRecoveryOptionsErrorKey:
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> (error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as?</span> RecoverableError)?<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>recoveryOptions
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> NSRecoveryAttempterErrorKey:
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">is</span> RecoverableError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">?</span> RecoveryAttempter() <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">nil</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">default</span>:
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">guard</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> customUserInfoError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as?</span> CustomNSError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">else</span> { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">nil</span> }
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> customUserInfoError<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>userInfo[key]
}
}</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">On platforms that predate the introduction of user info value providers, there are alternate implementation strategies, including introducing a custom <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSDictionary</code> subclass to use as the <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">userInfo</code> in the <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> that lazily populates the dictionary by, effectively, calling the <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">userInfoValueProvider</code> function above for each requested key. Or, one could eagerly populate <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">userInfo</code> on older platforms.</p><h3 style="box-sizing: border-box; margin-top: 1em; margin-bottom: 16px; line-height: 1.43; font-size: 1.5em; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; background-color: rgb(255, 255, 255);" class=""><a id="user-content-importing-error-types-from-objective-c" class="anchor" href="https://github.com/DougGregor/swift-evolution/blob/nserror-bridging/proposals/0000-nserror-bridging.md#importing-error-types-from-objective-c" aria-hidden="true" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none; display: inline-block; padding-right: 2px; margin-left: -18px; line-height: 1.2;"><svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Importing error types from Objective-C</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">In Objective-C, error domains are typically constructed using an enumeration describing the error codes and a constant describing the error domain, e.g,</p><div class="highlight highlight-source-objc" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">extern</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">NSString</span> *<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">const</span> AVFoundationErrorDomain;
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">typedef</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">NS_ENUM</span>(<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">NSInteger</span>, AVError) {
AVErrorUnknown = -<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">11800</span>,
AVErrorOutOfMemory = -<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">11801</span>,
AVErrorSessionNotRunning = -<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">11803</span>,
AVErrorDeviceAlreadyUsedByAnotherSession = -<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">11804</span>,
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// ...</span>
}</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">This is currently imported as an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">enum</code> that conforms to <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">Error</code>:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">enum</span> AVError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span> {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> unknown <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">-11800</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> outOfMemory <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">-11801</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> sessionNotRunning <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">-11803</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> deviceAlreadyUsedByAnotherSession <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">-11804</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">static</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> _domain: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span> { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> AVFoundationErrorDomain }
}</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">and Swift code introduces an extension that makes it an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">Error</code>, along with some implementation magic to allow bridging from an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> (losing <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">userInfo</code> in the process):</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">extension</span> AVError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> Error {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">static</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> _domain: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span> { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> AVFoundationErrorDomain }
}</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">Instead, error enums should be expressed with a new macro, <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NS_ERROR_ENUM</code>, that ties together the code and domain in the Objective-C header:</p><div class="highlight highlight-source-objc" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">extern</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">NSString</span> *<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">const</span> AVFoundationErrorDomain;
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">typedef</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">NS_ERROR_ENUM</span>(<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">NSInteger</span>, AVError, AVFoundationErrorDomain) {
AVErrorUnknown = -<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">11800</span>,
AVErrorOutOfMemory = -<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">11801</span>,
AVErrorSessionNotRunning = -<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">11803</span>,
AVErrorDeviceAlreadyUsedByAnotherSession = -<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">11804</span>,
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// ...</span>
}</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">This will import as a new struct <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">AVError</code> that contains an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code>, so there is no information loss. The actual enum will become a nested type <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">Code</code>, so that it is still accessible. The resulting struct will be as follows:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">struct</span> AVError {
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// Stored NSError. Note that error.domain == AVFoundationErrorDomain is an invariant.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">private</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> error: NSError
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// Describes the error codes; directly imported from AVError</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">enum</span> Code <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span>, ErrorCodeProtocol {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">typealias</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">ErrorType</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> AVError
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> unknown <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">-11800</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> outOfMemory <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">-11801</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> sessionNotRunning <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">-11803</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> deviceAlreadyUsedByAnotherSession <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">-11804</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">errorMatchesCode</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">_</span> <span class="pl-smi" style="box-sizing: border-box;">error</span>: AVError) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Bool</span> {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> error<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>code <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">==</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">self</span>
}
}
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// Allow one to create an error (optionally) with a userInfo dictionary.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">init</span>(_ code: Code, userInfo: [NSObject: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">AnyObject</span>] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> [<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span>]) {
error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> NSError(code: code<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">rawValue</span>, domain: _domain, userInfo: userInfo)
}
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// Retrieve the code.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> code: Code { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> Code(rawValue: error<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>code)<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">!</span> }
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// Allow direct access to the userInfo dictionary.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> userInfo: [NSObject: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">AnyObject</span>] { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> error<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>userInfo }
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// Make it easy to refer to constants without context.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">static</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> unknown: Code <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>unknown
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">static</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> outOfMemory: Code <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>outOfMemory
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">static</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> sessionNotRunning: Code <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>sessionNotRunning
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">static</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> deviceAlreadyUsedByAnotherSession: Code <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>deviceAlreadyUsedByAnotherSession
}
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// Implementation detail: makes AVError conform to Error</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">extension</span> AVError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> Error {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">static</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> _domain: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span> { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> AVFoundationErrorDomain }
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> _code: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span> { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> error<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>code }
}</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">This syntax allows one to throw specific errors fairly easily, with or without <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">userInfo</code> dictionaries:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">throw</span> AVError(<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>sessionNotRunning)
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">throw</span> AVError(<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>sessionNotRunning, userInfo: [ <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">...</span> ])</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">The <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">ImportedErrorCode</code> protocol is a helper so that we can define a general <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">~=</code> operator, which is used by both <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">switch</code>case matching and <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">catch</code> blocks:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">protocol</span> ErrorCodeProtocol {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">typealias</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">ErrorType</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> Error
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">errorMatchesCode</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">_</span> <span class="pl-smi" style="box-sizing: border-box;">error</span>: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">ErrorType</span>) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Bool</span>
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">~=</span> <EC: ErrorCodeProtocol> (error: Error, code: EC) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Bool</span> {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">guard</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> myError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as?</span> EC<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">ErrorType</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">else</span> { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">false</span> }
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> code<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>errorMatchesCode(myError)
}</pre></div><h3 style="box-sizing: border-box; margin-top: 1em; margin-bottom: 16px; line-height: 1.43; font-size: 1.5em; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; background-color: rgb(255, 255, 255);" class=""><a id="user-content-mapping-nserror-types-back-into-swift" class="anchor" href="https://github.com/DougGregor/swift-evolution/blob/nserror-bridging/proposals/0000-nserror-bridging.md#mapping-nserror-types-back-into-swift" aria-hidden="true" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none; display: inline-block; padding-right: 2px; margin-left: -18px; line-height: 1.2;"><svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Mapping <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: inherit; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> types back into Swift</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">When an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> object bridged to an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">Error</code> instance, it may be immediately mapped back to a Swift error type (e.g., if the error was created as a <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">HomeworkError</code> instance in Swift and then passed through <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> unmodified) or it might be leave as an instance of <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code>. The error might then be catch as a particular Swift error type, e.g.,</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">catch</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as</span> AVError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">where</span> error<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>code <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">==</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>sessionNotRunning {
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// able to access userInfo here!</span>
}</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">In this case, the mapping from an <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> instance to <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">AVError</code> goes through an implementation-detail protocol<code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">_ObjectiveCBridgeableError</code>:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">protocol</span> _ObjectiveCBridgeableError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> Error {
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// Produce a value of the error type corresponding to the given NSError,</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/// or return nil if it cannot be bridged.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">init</span>?(_bridgedNSError error: NSError)
}</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">The initializer is responsible for checking the domain and (optionally) the code of the incoming <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> to map it to an instance of the Swift error type. For example, <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">AVError</code> would adopt this protocol as follows:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// Implementation detail: makes AVError conform to _ObjectiveCBridgeableError</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">extension</span> AVError <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> _ObjectiveCBridgeableError {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">init</span>?(_bridgedNSError error: NSError) {
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// Check whether the error comes from the AVFoundation error domain</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">if</span> error<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>domain <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">!=</span> AVFoundationErrorDomain { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">nil</span> }
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// Save the error</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">self</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>error <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> error
}
}</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">We do not propose that <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">_ObjectiveCBridgeableError</code> become a public protocol, because the core team has already deferred a similar proposal (<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0058-objectivecbridgeable.md" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none;" class="">SE-0058</a>) to make the related protocol <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">_ObjectiveCBridgeable</code> public. </p><h2 style="box-sizing: border-box; margin-top: 1em; margin-bottom: 16px; line-height: 1.225; font-size: 1.75em; padding-bottom: 0.3em; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(238, 238, 238); color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; background-color: rgb(255, 255, 255);" class=""><a id="user-content-other-issues" class="anchor" href="https://github.com/DougGregor/swift-evolution/blob/nserror-bridging/proposals/0000-nserror-bridging.md#other-issues" aria-hidden="true" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none; display: inline-block; padding-right: 2px; margin-left: -18px; line-height: 1;"><svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Other Issues</h2><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class=""><code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> codes and domains are important for localization of error messages. This is barely supported today by <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">genstrings</code>, but becomes considerably harder when the domain and code are hidden (as they are in Swift). We would need to consider tooling to make it easier to localize error descriptions, recovery options, etc. in a sensible way. Although this is out of the scope of the Swift language per se, it's an important part of the developer story.</p><h2 style="box-sizing: border-box; margin-top: 1em; margin-bottom: 16px; line-height: 1.225; font-size: 1.75em; padding-bottom: 0.3em; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(238, 238, 238); color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; background-color: rgb(255, 255, 255);" class=""><a id="user-content-impact-on-existing-code" class="anchor" href="https://github.com/DougGregor/swift-evolution/blob/nserror-bridging/proposals/0000-nserror-bridging.md#impact-on-existing-code" aria-hidden="true" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none; display: inline-block; padding-right: 2px; margin-left: -18px; line-height: 1;"><svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Impact on existing code</h2><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">This is a major source-breaking change for Objective-C APIs that operate on <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> values, because those parameter/return/property types will change from <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> to <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">Error</code>. There are ~400 such APIs in the macOS SDK, and closer to 500 in the iOS SDK, which is a sizable number. Fortunately, this is similar in scope to the <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0069-swift-mutability-for-foundation.md" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none;" class="">Foundation value types proposal</a>, and can use the same code migration mechanism. That said, the scale of this change means that it should either happen in Swift 3 or not at all.</p><h2 style="box-sizing: border-box; margin-top: 1em; margin-bottom: 16px; line-height: 1.225; font-size: 1.75em; padding-bottom: 0.3em; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(238, 238, 238); color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; background-color: rgb(255, 255, 255);" class=""><a id="user-content-future-directions" class="anchor" href="https://github.com/DougGregor/swift-evolution/blob/nserror-bridging/proposals/0000-nserror-bridging.md#future-directions" aria-hidden="true" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none; display: inline-block; padding-right: 2px; margin-left: -18px; line-height: 1;"><svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Future directions</h2><h3 style="box-sizing: border-box; margin-top: 1em; margin-bottom: 16px; line-height: 1.43; font-size: 1.5em; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; background-color: rgb(255, 255, 255);" class=""><a id="user-content-better-tooling-for-describing-errors" class="anchor" href="https://github.com/DougGregor/swift-evolution/blob/nserror-bridging/proposals/0000-nserror-bridging.md#better-tooling-for-describing-errors" aria-hidden="true" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none; display: inline-block; padding-right: 2px; margin-left: -18px; line-height: 1.2;"><svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Better tooling for describing errors</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">When adopting one of the new protocols (e.g., <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">LocalizedError</code>) in an enum, one will inevitably end up with a number of <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">switch</code> statements that have to enumerate all of the cases, leading to a lot of boilerplate. Better tooling could improve the situation considerably: for example, one could use something like <a href="https://developer.apple.com/library/prerelease/content/documentation/MacOSX/Conceptual/BPInternational/StringsdictFileFormat/StringsdictFileFormat.html" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none;" class="">Cocoa's stringsdict files</a> to provide localized strings identified by the enum name, case name, and property. That would eliminate the need for the switch-on-all-cases implementations of each property.</p><h3 style="box-sizing: border-box; margin-top: 1em; margin-bottom: 16px; line-height: 1.43; font-size: 1.5em; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; background-color: rgb(255, 255, 255);" class=""><a id="user-content-round-tripping-errors-through-userinfo" class="anchor" href="https://github.com/DougGregor/swift-evolution/blob/nserror-bridging/proposals/0000-nserror-bridging.md#round-tripping-errors-through-userinfo" aria-hidden="true" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none; display: inline-block; padding-right: 2px; margin-left: -18px; line-height: 1.2;"><svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Round-tripping errors through <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: inherit; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">userInfo</code></h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">The <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">CustomNSError</code> protocol allows one to place arbitrary key/value pairs into <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code>'s <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">userInfo</code> dictionary. The implementation-detail <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">_ObjectiveCBridgeableError</code> protocol allows one to control how a raw <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> is mapped to a particular error type. One could effectively serialize the entire state of a particular error type into the <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">userInfo</code> dictionary via<code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">CustomNSError</code>, then restore it via <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">_ObjectiveCBridgeableError</code>, allowing one to form a complete <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> in Objective-C that can reconstitute itself as a particular Swift error type, which can be useful both for mixed-source projects and (possibly) as a weak form of serialization for <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code>s.</p><h2 style="box-sizing: border-box; margin-top: 1em; margin-bottom: 16px; line-height: 1.225; font-size: 1.75em; padding-bottom: 0.3em; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(238, 238, 238); color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; background-color: rgb(255, 255, 255);" class=""><a id="user-content-alternatives-considered" class="anchor" href="https://github.com/DougGregor/swift-evolution/blob/nserror-bridging/proposals/0000-nserror-bridging.md#alternatives-considered" aria-hidden="true" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none; display: inline-block; padding-right: 2px; margin-left: -18px; line-height: 1;"><svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Alternatives considered</h2><h3 style="box-sizing: border-box; margin-top: 1em; margin-bottom: 16px; line-height: 1.43; font-size: 1.5em; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; background-color: rgb(255, 255, 255);" class=""><a id="user-content-exposing-the-domain-code-and-user-info-dictionary-directly" class="anchor" href="https://github.com/DougGregor/swift-evolution/blob/nserror-bridging/proposals/0000-nserror-bridging.md#exposing-the-domain-code-and-user-info-dictionary-directly" aria-hidden="true" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none; display: inline-block; padding-right: 2px; margin-left: -18px; line-height: 1.2;"><svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Exposing the domain, code, and user-info dictionary directly</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">This proposal does not directly expose the domain, code, or user-info dictionary on <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">ErrorProtocol</code>, because these notions are superseded by Swift's strong typing of errors. The domain is effectively subsumed by the type of the error (e.g., a Swift-defined error type uses its mangled name as the domain); the code is some type-specific value (e.g., the discriminator of the enum); and the user-info dictionary is an untyped set of key-value pairs that are better expressed in Swift as data on the specific error type.</p><h3 style="box-sizing: border-box; margin-top: 1em; margin-bottom: 16px; line-height: 1.43; font-size: 1.5em; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; background-color: rgb(255, 255, 255);" class=""><a id="user-content-bridging-nserror-to-a-new-value-type-error" class="anchor" href="https://github.com/DougGregor/swift-evolution/blob/nserror-bridging/proposals/0000-nserror-bridging.md#bridging-nserror-to-a-new-value-type-error" aria-hidden="true" style="box-sizing: border-box; background-color: transparent; -webkit-text-decoration-skip: objects; color: rgb(64, 120, 192); text-decoration: none; display: inline-block; padding-right: 2px; margin-left: -18px; line-height: 1.2;"><svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Bridging <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: inherit; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">NSError</code> to a new value type <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: inherit; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">Error</code></h3><div style="box-sizing: border-box; margin-top: 0px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255); margin-bottom: 0px !important;" class="">One could introduce a new value type, <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">Error</code>, that stores a domain, code, and user-info dictionary but provides them with value semantics. Doing so would make it easier to create "generic" errors that carry some information. However, we feel that introducing new error types in Swift is already easier than establishing a new domain and a set of codes, because a new enum type provides this information naturally in Swift.</div></div></div></div></blockquote></div><br class=""></body></html>