Skip to content

Instantly share code, notes, and snippets.

@bocato
Created March 25, 2020 13:19
Show Gist options
  • Save bocato/0f62dca70f6a513ce11175e9dbc25f82 to your computer and use it in GitHub Desktop.
Save bocato/0f62dca70f6a513ce11175e9dbc25f82 to your computer and use it in GitHub Desktop.
extension NSError {
/// A convenience initializer for NSError to set its description.
///
/// - Parameters:
/// - domain: The error domain.
/// - code: The error code.
/// - description: Some description for this error.
convenience init(domain: String, code: Int, description: String) {
self.init(domain: domain, code: code, userInfo: [(kCFErrorLocalizedDescriptionKey as CFString) as String: description])
}
}
// Usage
let myError = NSError(
domain: "SomeDomain",
code: 123,
description: "Some description."
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment