How to provide a localized description with an Error type in Swift?
<p>I am defining a custom error type with Swift 3 syntax and I want to provide a user-friendly description of the error which is returned by the <code>localizedDescription</code> property of the <code>Error</code> object. How can I do it? </p>

<pre><code>public enum MyError: Error {
  case customError

  var localizedDescription: String {
    switch self {
    case .customError:
      return NSLocalizedString("A user-friendly description of the error.", comment: "My error")
    }
  }
}

let error: Error = MyError.customError
error.localizedDescription
// "The operation couldn’t be completed. (MyError error 0.)"
</code></pre>

<p>Is there a way for the <code>localizedDescription</code> to return my custom error description ("A user-friendly description of the error.")? Note that the error object here is of type <code>Error</code> and not <code>MyError</code>. I can, of course, cast the object to MyError</p>

<pre><code>(error as? MyError)?.localizedDescription
</code></pre>

<p>but is there a way to make it work without casting to my error type?</p>