In an Apple official document, adopting modern Objective-c, "Adapting to Modern Objectice-c" mentions the use of Ns_enum and ns_options to declare enumerations
Ns_enum is used to declare enumerations based on generic shaping, whereas ns_options is typically used to declare a bitmask-based declaration (bitmask)
Often mentioned in the General tutorial book, but what is the difference?
The following macro definitions are available in the NSObjCRuntime.h
[CPP]View PlainCopy
- #define NS_ENUM (_type, _name) cf_enum (_type, _name)
[CPP]View PlainCopy
- #define Ns_options (_type, _name) cf_options (_type, _name)
That is, Ns_enum, Cf_enum is the same, ns_options, Cf_options is the same
So, what exactly is the Cf_enum,cf_options macro?
The following macro definitions are in the Cf_enum,cf_options macro definition header file CFAvailability.h
[CPP]View Plain Copy
- #if (__cplusplus && __cplusplus >= 201103L && (__has_extension (cxx_strong_enums) | | __has_feature ( objc_fixed_enum))) | | (!__cplusplus && __has_feature (objc_fixed_enum))
- #define CF_ENUM (_type, _name) ENUM _name: _type _name; Enum _name: _type
- #if (__cplusplus)
- #define Cf_options (_type, _name) _type _name; Enum: _type
- #else
- #define Cf_options (_type, _name) enum _name: _type _name; Enum _name: _type
- #endif
- #else
- #define CF_ENUM (_type, _name) _type _name; Enum
- #define Cf_options (_type, _name) _type _name; Enum
- #endif
Fraud, in fact, the basic idea is no difference, and did not say that the elements of Ns_enum enumeration must be integral type, the elements of Ns_options enumeration must be related to the bit, according to its fundamental
The purpose of the enumeration is to add a name to the integer data. Ns_enum,ns_options just for the unity of the Code, to constrain the coding rules.
However, a closer look will find different, why sometimes the macro is defined as # define Cf_options (_type, _name) _type _name; Enum: _type or # define Cf_options (_type, _name) enum _name: _type _name; Enum _name: _type
StackOverflow inside the question said, but the first few answers seem a bit simple, not to mention the point, the last one seems to be very reasonable
Specifically, because C++11 supports the forward declaration of enumerations (forward declare), the resulting bug, used for compatibility with classes such as "Cf_options (_type, _name) enum _name: _type _name; Enum _name: _type "
The difference between Ns_enum and ns_options