Introduction:
Enumeration Value It is an integer (int) and, it is not involved in memory consumption and deallocation , The enumeration definition variable can be used directly without initialization.
There is only one purpose of using enumerations in code, which is to increase the readability of the code.
Use:
The enumeration is defined as follows:
[CSharp]View Plaincopy < param name= "wmode" value= "Transparent" >
- 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 ):
[CSharp]View Plaincopy < param name= "wmode" value= "Transparent" >
- 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 after
[CSharp]View Plaincopy
- 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-in
- };
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, don't produce two of the same numbers.
Manually to create a bit operation enumeration, and a little bit of time, fortunately Apple has prepared a uintfor us. So, initialize a bitwise operation enumeration in this way:
[CSharp]View Plaincopy
- typedef ns_enum (UINT, Test)
- {
- TestA,
- TESTB,
- TESTC,
- TestD,
- Teste
- };
Multiple enumeration values are assigned as follows:
[CSharp]View Plaincopy
- 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:
[CSharp]View Plaincopy
- 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 (@"there");
- }Else
- {
- NSLog (@"no");
- }
- NSLog (@"%d", (TES & TESTB));
- if (TES & TestA)) {
- NSLog (@"there");
- }Else
- {
- NSLog (@"no");
- }
- NSLog (@"%d", (TES & TESTC));
- if (TES & TESTC)) {
- NSLog (@"there");
- }Else
- {
- NSLog (@"no");
- }
If not included, returns 0, 0 indicates false no then goes to else
It is also possible to accumulate a value for an enumeration variable at any time, but to control not to add an enumerated value that has already been added, the value of the enumeration variable will not change, but it will mislead the person reading the Code
[CSharp]View Plaincopy
- TES |=TESTC;
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.
[CSharp]View Plaincopy
- Tes^= Teste;
The use of iOS for enumerations