Enumerations are very useful in development, and are used to represent states, options, and status codes. We know that object-C is based on C, so it has all the functions of C, and enumeration type is one of them. Enumeration is a constant naming method, and the various states experienced by an Object can be defined as an enumeration set. For example, the state of eating during the day:
enum EatSomething {
EatBreakfast,
EatLunch,
EatDinner,
};
Copy the code
But defining enumerations in code like this is not very neat:
enum EatSonmething eatState = EatLunch;
Copy the code
To simplify the process of defining enumerations, we need to take an extra step in defining enumerations, using the typedef keyword to redefine enumerations:
enum EatSomething {
EatBreakfast,
EatLunch,
EatDinner,
};
typedef enum EatSomething EatSonmething;
Copy the code
Now we can easily define enumeration types:
EatSonmething eatState = EatLunch;
Copy the code
Each state is represented by an easy-to-understand value, making the code more readable. And the editor automatically assigns a number to each state, starting from zero and increasing by one. If you want to change the serial number:
enum EatSomething { EatBreakfast = 2, EatLunch, EatDinner, }; /* the first term starts at 2 and increments by 1 to 3,4 */Copy the code
C++11 fixes certain features of enumerations. For one thing, you can specify what “underlying data type” is used to hold the variables of an enumeration type. This allows enumeration types to be declared forward, otherwise we have no way of knowing the type of an enumeration, its data type size, and space allocation. To specify the type used:
enum EatSomething : NSInteger{
/**/
};
Copy the code
Sometimes, when we resize the view horizontally or vertically, we encounter several scenarios where enumeration types are used in combination with bitwise or operators:
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
Copy the code
Starting with the second, integer powers of 2, starting with 0, they can be combined as needed, such as whether to enable an option:
UIViewAutoresizing resizing = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
if(resizing & UIViewAutoresizingFlexibleWidth){
//UIViewAutoresizingFlexibleWidth is set
}
Copy the code
System framework of frequent use of this method, in UIKit UI framework and a support device shows the direction of the enumerated type called UIInterfaceOrientationMask, need to implement a method, called supportedInterfaceOrientations Tell the system the display direction to be supported:
/ / support screen direction, this method returns the value of UIInterfaceOrientationMask type. - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft; }Copy the code
Here you will notice that the enumeration type defined by the blogger above is different:
Enum name {}.. typedef NS_OPTIONS(type, name){}Copy the code
The Foundation framework defines auxiliary macros that specify the underlying data type to hold enumeration values when defining enumeration types, with backward compatibility. If the editor of the target platform supports the new standard, use the new syntax, otherwise use the old syntax. These macros are defined using the #define preprocessor directive. To define an enumeration type of eating state and an enumeration type like UIViewAutoresizing that contains a series of options:
typedef NS_ENUM(NSUInteger, EatSomething) {
EatBreakfast,
EatLunch,
EatDinner,
};
typedef NS_OPTIONS(NSUInteger, LHHViewDirection) {
LHHViewUp =1 << 0,
LHHViewDown =1 << 1,
LHHViewLeft =1 << 2,
LHHViewRight =1 << 3,
};
Copy the code
If the enumeration can be bitwise or combined, use NS_OPTIONS to define it. Otherwise, use NS_ENUM to define it.
Finally, one thing to be aware of when using enumerations, especially in Switch, is the habit of adding a default branch. When you add a new state, the editor will warn you that the new state has not been processed in the switch branch. If you add a default branch, the editor will handle the new state without warning. This is especially important when using enumerations, usually to ensure that the Switch statement can handle all styles.