The difference between Ns_enum and ns_options

Source: Internet
Author: User

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
    1. #define NS_ENUM (_type, _name) cf_enum (_type, _name)
[CPP]View PlainCopy
    1. #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
  1. #if (__cplusplus && __cplusplus >= 201103L && (__has_extension (cxx_strong_enums) | | __has_feature ( objc_fixed_enum))) | | (!__cplusplus && __has_feature (objc_fixed_enum))
  2. #define CF_ENUM (_type, _name) ENUM _name: _type _name; Enum _name: _type
  3. #if (__cplusplus)
  4. #define Cf_options (_type, _name) _type _name; Enum: _type
  5. #else
  6. #define Cf_options (_type, _name) enum _name: _type _name; Enum _name: _type
  7. #endif
  8. #else
  9. #define CF_ENUM (_type, _name) _type _name; Enum
  10. #define Cf_options (_type, _name) _type _name; Enum
  11. #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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.