Summary of enumeration usage developed by iOS

  • 2021-01-18 06:39:21
  • OfStack

preface

Enum, also known as enumeration, has existed since the beginning of C. C++, Java, Objective-C, Swift, of course, all have corresponding enumeration types. There may be more or less functions, but the core is still one - the state, options and other "constants" in the standard definition code.

For example, if we want to do something different with four seasons in development, the first thing we might want to do is define a variable of type int and assign different values to the variable to represent the four seasons (e.g.1 for spring,2 for summer,3 for fall, and 4 for winter). However, the number 1234 does not immediately make sense to the person reading it. At this point, you choose to use an enumeration to address this requirement

Enumeration in C

Enumerated types are defined in the C language as follows


enum  The enumeration name 
{
   identifier  =  Integer constants ,
   identifier  =  Integer constants ,
   identifier  =  Integer constants ,
   identifier  =  Integer constants 
};

For example, "Define an enumeration type with an enumeration name of Test and an enumeration type with an enumeration member of TestA, TestB, etc." is defined as follows


enum Test
{
  TestA = 0,
  TestB = 1,
  TestC = 2,
  TestD = 3
};

Note 1: When none of the enumerators is set to "= integer constant ", by default the enumeration starts at the first identifier and increments by 1 from 0

Note 2: When an enumerator is set to the "= integer constant ", the following identifiers are incremented by 1

To use this enumeration type, we need to create one enumeration variable


enum  The enumeration name   Enumeration variable  =  Enumerated variable values ;

For example, "Create an enumeration variable test with enumeration name Test and assign it the value TestB"


enum Test test = TestB;

Note: The value of an enumeration variable can only be selected from the enumeration members of the corresponding enumeration type

We can also define an enumeration variable at the same time as the enumeration type. The enumeration variable is not initialized and can be assigned and used directly


enum Test
{
  TestA = 0,
  TestB,
  TestC,
  TestD
} test;

Note: You can omit the enumeration name and define the enumeration variables directly (called "anonymous enumeration ").

Set an "alias" for an enumerated type with typedef, so that you can use enumerated types as if they were int1


typedef enum _Test
{
  TestA = 0,
  TestB,
  TestC,
  TestD
} Test;

Once the "alias" is defined, enumerated variables can be defined as follows


Test test = TestB;

For anonymous enumeration, setting the "alias" via typedef will look like this


typedef enum
{
  TestA = 0,
  TestB,
  TestC,
  TestD
} Test;

Once the "alias" is defined, enumerated variables can be defined as follows


Test test = TestB;

Enumeration in OC

In Objective-C,Apple introduces two macros in iOS6 to redefine enumeration types (that is, NS_ENUM and NS_OPTIONS). There is no difference between these two macros in essence, both are used to define enumeration types, but in use NS_ENUM is mostly used for general enumeration, while NS_OPTIONS is mostly used for enumeration with shift operation

NS_ENUM usage example


typedef NS_ENUM(NSInteger, Test)
{
  TestA = 0,
  TestB,
  TestC,
  TestD
};

NS_OPTIONS usage example


enum Test
{
  TestA = 0,
  TestB = 1,
  TestC = 2,
  TestD = 3
};
0

enum Test
{
  TestA = 0,
  TestB = 1,
  TestC = 2,
  TestD = 3
};
1

Enumeration with a shift to more with an enumeration variable can be assigned multiple enumeration members at the same time, its principle is to each enumeration value bitwise or (|), because of a shift enumeration members can guarantee the bitwise or (|) calculation results after only 1, so each outcome can be backward calculated by which a few members of the enumeration bitwise or (|)

"Using the bitwise or (|) for the enumeration variable test TestA assignment enumeration members at the same time, TestB and TestC" as an example


enum Test
{
  TestA = 0,
  TestB = 1,
  TestC = 2,
  TestD = 3
};
2

Take "Remove 1 enumerator TestC by using bitwise XOR (^) as the enumeration variable test"


Test test = TestA | TestB | TestC;
test ^= TestC;

Note: If the enumerator test itself is not assigned to TestC, using bitwise XOR (^) will assign one more enumerator, TestC, to the enumerator test
Start with "use bitwise with ( & TestA" is assigned to the enumeration member TestA"


enum Test
{
  TestA = 0,
  TestB = 1,
  TestC = 2,
  TestD = 3
};
4

conclusion

The above is the whole content of this article, I hope to bring you a certain help in your study or work, if you have any questions, you can leave a message to exchange.


Related articles: