Introduction:
EnumerationValueIt isINTEGER (INT)In addition, it is not involved in memory usage and release,You can use enumeration variables directly without initialization.
Only one purpose of enumeration is to increase the readability of the Code.
Usage:
EnumerationIs defined as follows:
Typedef Enum {// The following is the enumerated member Testa = 0, testb, testc, testd} test; // enumeration name
It can also be defined as follows (Recommendation: clear structure):
Typedef ns_enum (nsinteger, test1) {// The following enumerated members are test1a = 0, test1b = 1, test1c = 2, test1d = 3 };
Enumeration definition is also supported.Bitwise operationIs defined as follows:
The value after the equal sign must be 1.
Typedef ns_enum (nsinteger, test) {Testa = 1, // 1 1 testb = 1 <1, // 2 2 2 10 to 10 in hexadecimal format 2 testc = 1 <2, // 4 3 100 to 10 in hexadecimal format 4 testd = 1 <3, // 8 4 1000 to 10 in hexadecimal format 8 teste = 1 <4/16 5 10000 to 10 in hexadecimal notation 16 };
When will this method be used?
That is, when an enumerated variable may represent multiple enumerated values, the principle is to add up each enumerated value when multiple enumerated values are assigned to an enumerated variable.
After adding up, a new value is obtained. In order to ensure the uniqueness of this value, the bit operation plays an important role.
The bitwise operation ensures the uniqueness of the enumerated value combination.
The bitwise operation converts binary to decimal. That is to say, the enumerated value contains the computed decimal value.
For example:
After the enumeration is set through the bitwise operation above, the printed enumerated values are: 1 2 4 8 16
No matter how you combine these five numbers, no two identical numbers will be generated.
It takes a little time to manually create bitwise operation enumeration. Fortunately, Apple has preparedUint. So, use the following method to initialize a single-bit operation enumeration:
typedef NS_ENUM(uint, Test){ TestA, TestB, TestC, TestD, TestE };
You can assign values to multiple enumerated values as follows:
Test tes = (TestA|TestB);
To determine whether an enumerated variable contains a fixed enumerated value, you must ensure that the enumerated value and the uniqueness of each combination before use:
Nslog (@ "% d", Testa, testb, testc, testd, teste); test tes = (testa | testb ); nslog (@ "% d", Tes); nslog (@ "% d", (TES & Testa); If (TES & Testa )) {nslog (@ "") ;}else {nslog (@ "") ;}nslog (@ "% d", (TES & testb )); if (TES & Testa) {nslog (@ "");} else {nslog (@ "");} nslog (@ "% d ", (TES & testc); If (TES & testc) {nslog (@ "");} else {nslog (@ "");}
If it is not included, 0 is returned, and 0 indicates false No.
You can also add a value to the enumerated variable at any time, but you need to control it by yourself. Do not add the enumerated value that has already been added. The enumerated variable value will not change, but this will mislead the person who reads the code.
tes |=TestC;
If there are accumulated values, there will naturally be a progressive reduction. If there are non-existent enumeration values, the enumerated values of this progressive reduction will be automatically accumulated.
tes^= TestE;
The above is almost finished. If you have any questions, please feel free to ask.