Ios/object-c: Enum type Enum,ns_enum,ns_options

Source: Internet
Author: User
Tags binary to decimal

In general, we use the C-style enum keyword to define enumeration types.

[CPP]View Plaincopy
    1. enum{
    2. Uiviewanimationtransitionnone,
    3. Uiviewanimationtransitionflipfromleft,
    4. Uiviewanimationtransitionflipfromright,
    5. Uiviewanimationtransitioncurlup,
    6. Uiviewanimationtransitioncurldown,
    7. } uiviewanimationtransition;
[CPP]View Plaincopy
  1. Displacement Operation Enumeration Definition
  2. enum {
  3. Uiviewautoresizingnone = 0,
  4. Uiviewautoresizingflexibleleftmargin = 1 << 0,
  5. Uiviewautoresizingflexiblewidth = 1 << 1,
  6. Uiviewautoresizingflexiblerightmargin = 1 << 2,
  7. Uiviewautoresizingflexibletopmargin = 1 << 3,
  8. Uiviewautoresizingflexibleheight = 1 << 4,
  9. Uiviewautoresizingflexiblebottommargin = 1 << 5
  10. };
  11. typedef Nsuinteger Uiviewautoresizing;  //Use Nsuinteger where you can use uiviewautoresizing,//uiviewautoresizing equivalent to an alias of Nsuinteger.
  12. So a uiviewautoresizing variable can be assigned directly to Nsuinteger.

An enumeration value is typically an int value of 4 bytes, which is 8 bytes on a 64-bit system.

After IOS6 and Mac OS 10.8, Apple introduced two macros to redefine the two enum types, essentially combining the enum definition with a typedef and using different macros to differentiate from the code perspective.

Ns_options is generally used to define the enumeration values of displacement-related operations, we can refer to the Uikit.framework header file, you can see a large number of enumeration definitions.

[CPP]View Plaincopy
  1. typedef ns_enum (Nsinteger, uiviewanimationtransition) {
  2. Uiviewanimationtransitionnone,//default starting from 0
  3. Uiviewanimationtransitionflipfromleft,
  4. Uiviewanimationtransitionflipfromright,
  5. Uiviewanimationtransitioncurlup,
  6. Uiviewanimationtransitioncurldown,
  7. };
  8. typedef ns_options (Nsuinteger, uiviewautoresizing) {
  9. Uiviewautoresizingnone = 0,
  10. Uiviewautoresizingflexibleleftmargin = 1 << 0,
  11. Uiviewautoresizingflexiblewidth = 1 << 1,
  12. Uiviewautoresizingflexiblerightmargin = 1 << 2,
  13. Uiviewautoresizingflexibletopmargin = 1 << 3,
  14. Uiviewautoresizingflexibleheight = 1 << 4,
  15. Uiviewautoresizingflexiblebottommargin = 1 << 5
  16. };

The two macros are defined in Foundation.framework's NSObjCRuntime.h:

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

Will

[CPP]View Plaincopy
    1. typedef ns_enum (Nsinteger, uiviewanimationtransition) {
Expand to get: [CPP]View Plaincopy
    1. typedef enum Uiviewanimationtransition:nsinteger uiviewanimationtransition;
    2. Enum Uiviewanimationtransition:nsinteger {

From the enumeration definition, the essence of Ns_enum and Ns_options is the same, just literally distinguishing its use. Ns_enum is a general case, ns_options is generally used to define situations with displacement operations or characteristics (bitmask).

When actually used, you can define directly:

[CPP]View Plaincopy
    1. typedef Enum:nsinteger {...}  Uiviewanimationtransition;

is equivalent to the definition above.

Reference Documentation:

1. http://nshipster.com/ns_enum-ns_options/

2.http://iamthewalr.us/blog/2012/11/ns_enum-and-ns_options/

Original: http://blog.csdn.net/annkie/article/details/9877643

Enumeration is very important, especially in the early application development, the server-side data format needs to be changed, the enumeration and macros can be the program concise, and small changes.

Online has a personal writing concise, suitable for beginners

Transferred from: http://blog.csdn.net/ysy441088327/article/details/8012677

Preface: Egg-Ache enumeration, do not underestimate! Get to the point: the first thing to know is that the enumeration value is a shape (int) and that it does not participate in memory consumption and frees enumeration definition variables to be used directly without initialization. The enumeration is defined as follows:

typedef enum {

The following is an enumeration member TestA = 0,

TESTB,

TESTC,

TestD

}TEST;//Enumeration Name

can also be defined as follows (recommended: the structure is clearer):

typedef ns_enum (Nsinteger, Test1) {

The following is an enumeration member

TEST1A = 0,

TEST1B = 1,

test1c = 2,

TEST1D = 3

};

The definition of an enumeration also supports the way in which bit operations are defined, as follows: The equals sign must be equal to 1

typedef ns_enum (Nsinteger, Test) {

TestA = 1,//1 1 1

TESTB = 1 << 1,//2 2 10 converted to 10 binary 2

TESTC = 1 << 2,//4 3 100 converted to 10 binary 4

TestD = 1 << 3,//8 4 1000 converted to 10 Binary 8

Teste = 1 << 4//16 5 10000 converted to 10 binary 16

};

When do you want to use this method? That's when an enumeration variable might represent multiple enumeration values. In fact, when assigning multiple enumeration values to an enumeration variable, the principle simply adds up the individual enumeration values. When you add up, you get a new value, and in order to ensure the uniqueness of this value, this time it is the important function of the bit operation. Bitwise operations ensure uniqueness of the combination of enumeration values. Because the bitwise operation is computed by converting the binary to decimal, that is, the computed decimal value is accessed in the enumeration value. To make an analogy: After the enumeration is set by the bitwise operation above, the printed enumeration values are: 1 2 4 8 16 These 5 numbers, no matter how you combine them, will not produce two of the same numbers.

Multiple enumeration values are assigned as follows:

Test tes = (testa| TESTB);

To determine if an enumeration variable contains a fixed enumeration value, you need to ensure the enumeration values and the uniqueness of each combination before use:

NSLog (@ "%d%d%d%d%d", testa,testb,testc,testd,teste);

Test tes = (testa| TESTB); NSLog (@ "%d", TES);

NSLog (@ "%d", (TES & TestA));

if (tes & TestA) {NSLog (@ "has");}

else {NSLog (@ "not");} NSLog (@ "%d", (TES & TESTB));

if (tes & TestA) {NSLog (@ "has");}

else {NSLog (@ "not");}

NSLog (@ "%d", (TES & TESTC));

if (tes & TESTC) {NSLog (@ "has");}

else {NSLog (@ "not");}

If not included, returns 0, 0 means false No then goes to else you can also accumulate a value for the enumeration variable at any time, but you have to control not to add an enumerated value that has already been added, the value of the enumeration variable will not change, but this will mislead the person reading the Code

There is accumulation, naturally have to lose, if the decrement of the enumeration value does not exist, then this time the decrement of the enumeration value will be automatically added up.

Tes^= Teste;

Related Article

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.