Skip to content

Instantly share code, notes, and snippets.

@bdash
Last active August 1, 2016 22:51
Show Gist options
  • Save bdash/bf29e26c429b78cc155f1a2e1d851f8b to your computer and use it in GitHub Desktop.
Save bdash/bf29e26c429b78cc155f1a2e1d851f8b to your computer and use it in GitHub Desktop.
Implementation of MY_ERROR_ENUM
// SE-0112 mentions an NS_ERROR_ENUM macro to convey the association between an error code enumeration and
// its associated error domain. Xcode 8 beta 4 includes the Swift changes from SE-0112, but does not appear
// to provide NS_ERROR_ENUM.
//
// NS_ERROR_ENUM is intended to expand to an enum with the ns_error_domain(…) attribute.
// Writing our own macro is complicated by the fact that the Swift Objective-C importer
// for Swift 2.3 gives special importing treatment to enums declared using NS_ENUM / CF_ENUM.
// This requires some hoop-jumping to ensure that the enum declared by our macro is handled
// nicely by both Swift 2.3 and Swift 3.0.
#if __has_attribute(ns_error_domain)
#define MY_ERROR_ENUM(type, name, domain) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wignored-attributes\"") \
NS_ENUM(type, __attribute__((ns_error_domain(domain))) name) \
_Pragma("clang diagnostic pop")
#else
#define MY_ERROR_ENUM(type, name, domain) NS_ENUM(type, name)
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment