Tuesday, February 4, 2020

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 NSError domains and code enums let the client benefit from all the completeness of NSError while writing first-class swift code.

While NSError initing remains very similar, their handling gets shortened and clarified.

Joachim Kurz:

What the Compiler does for the enum cases of an NS_ERROR_ENUM is 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 Code remove that suffix.

See also: Handling Cocoa Errors in Swift.

Comments RSS · Twitter

Leave a Comment