Objective
Enum, that is, the enumeration, from the C language, C + +, Java, objective-c, Swift These languages, of course, have the corresponding enumeration type, the function may have less, but the core is a-specification of the definition of the code in the State, options, and so on "constants."
For example, we want to do different things in development according to the changes of the seasons, our first thought might be to define an int variable, by assigning a different value to the variable to represent the Four seasons (e.g 1 for spring, 2 for summer, 3 for fall, 4 for winter). But for those who read the code, They are not able to understand exactly what the number 1234 represents in a short time. This time, choose to use an enumeration to respond to that requirement
Enumerations in the C language
The enumeration types are defined in the C language in the following ways
Enum enum name
{
identifier = integer constant,
identifier = integer constant,
identifier = integer constant,
identifier = integer constant
};
As an example of "defining an enumeration name named Test, with an enumeration type of enumeration members such as Testa, TESTB, and so on, the following is defined as
Enum Test
{
testa = 0,
testb = 1,
TESTC = 2,
testd = 3
};
Note 1: When an enumeration member does not set "= Integer Constant", the default starts with the first identifier, and then increases by 1 from 0.
Note 2: When you set the "= Integer Constant" for an enumeration member, the identifier that follows will be incremented by 1 on this basis
When using this enumeration type, we need to create an enumeration variable
Enum Enum Name Enumeration variable = enumeration variable value;
As an example of "create an enumerated variable named test and assign it a value of Testb"
Note: The value of an enumeration variable can only be selected from an enumeration member of the corresponding enumeration type
We can also define an enumeration variable while defining the enumeration type, which is not initialized and can be directly assigned and used
Enum Test
{
testa = 0,
testb,
testc,
testd
} test;
Note: This enumeration name can be omitted and directly define an enumeration variable (called an "anonymous enumeration")
Set an "Alias" for an enumerated type by typedef so that you can use an enumeration type like an int
typedef enum _TEST
{
testa = 0,
testb,
testc,
testd
} Test;
After you define an alias, you can define an enumeration variable in the following ways
For anonymous enumerations, setting "aliases" through a typedef becomes the following form
typedef enum
{
testa = 0,
testb,
testc,
testd
} Test;
After you define an alias, you can define an enumeration variable in the following ways
Enumerations in the OC language
In the Objective-c language, Apple introduced two macros in IOS6 to redefine the enumeration type (that is, Ns_enum and ns_options), which are essentially the same and are used to define enumerated types, but in use Ns_enum are used for general enumerations, And ns_options is used for enumerations with shift operations.
Ns_enum Use Example
typedef ns_enum (Nsinteger, Test)
{
testa = 0,
testb,
testc,
testd
};
Ns_options Use Example
typedef ns_options (Nsuinteger, Test) {
testa = 1 << 0,
testb = 1 << 1,
TESTC = 1 << 2,
TESTD = 1 << 3
};
typedef ns_options (Nsuinteger, Test) {
Testnone = 0,
testa = 1 << 0,
testb = 1 << 1,
TESTC = 1 << 2,
testd = 1 << 3
};
Enumerations with shift operations are used in situations where the same enumeration variable can simultaneously assign multiple enumeration members, by bitwise OR (|), because the enumeration members of the shift operation can guarantee that bitwise OR (|) The uniqueness of the result after the calculation, so each result can be reversed to calculate which enumeration member is bitwise OR (|) and into
To "Use bitwise OR (|) Assigning enumeration members Testa, TESTB and TESTC "for enumeration variable test" as an example
Test test = Testa | TESTB;
Test |= TESTC;
Take the example of "Use bitwise XOR or (^) to remove an enumeration member TESTC" for enumeration variable test
Test test = Testa | Testb | TESTC;
Test ^= TESTC;
Note: If the enumeration variable test itself is not assigned a value of TESTC, then using bitwise XOR or (^) will assign an enumeration member to the enumeration variable test multiple TESTC
As an example of "Using Bitwise AND (&) to determine whether the enumeration variable test is assigned an enumeration member Testa"
Test test = Testa | TESTB;
if (Test & Testa)
{
NSLog (@ "yes");
}
else
{
NSLog (@ "no");
}
Summarize
The above is the entire content of this article, I hope to be able to learn or work to bring certain help, if you have questions you can message exchange.