NSErrorDomain, NS_ERROR_ENUM, and NSErrorUserInfoKey
Bertrand Longevialle (via Colin Cornaby):
Objective-C has a few macros to available to make your own NSError+YourDomain.[hm] self-documentingly crystal-clear. 👌
[…]
Because it is imported in Swift as a specific type, properly defined
NSErrordomains and code enums let the client benefit from all the completeness ofNSErrorwhile writing first-class swift code.While
NSErroriniting remains very similar, their handling gets shortened and clarified.
What the Compiler does for the enum cases of an
NS_ERROR_ENUMis two-fold:
- use the name of the enum and remove that prefix from all enum cases before importing them to swift
- create an enum with the given name to hold those cases. If the given name ends with
Coderemove that suffix.
See also: Handling Cocoa Errors in Swift.