Displacement enumeration
- Displacement enumeration is a very old C-language technique
- Bitwise AND if all is 1 the result is 1
- Bitwise OR if all is 0 the result is 0
Drill
1 Defining enumeration types
Action Type Enumeration
typedef enum {
Actiontypetop = 1 << 0,
Actiontypebottom = 1 << 1,
Actiontypeleft = 1 << 2,
Actiontyperight = 1 << 3
} ActionType;
Respond differently depending on the type of operation parameter
The type of operation can be any combination
-(void) Action: (ActionType) Type {
if (type = = 0) {
NSLog (@ "no operation");
Return
}
if (type & actiontypetop) {
NSLog (@ "Top%tu", type & Actiontypetop);
}
if (type & Actiontypebottom) {
NSLog (@ "Bottom%tu", type & Actiontypebottom);
}
if (type & Actiontypeleft) {
NSLog (@ "left%tu", type & Actiontypeleft);
}
if (type & actiontyperight) {
NSLog (@ "Right%tu", type & Actiontyperight);
}
}
ActionType type = Actiontypetop | Actiontyperight;
[Self action:type];
Code Summary
- Use a bitwise or can set multiple types for one parameter at the same time
- Use bitwise and can determine the specific type when executing
- With the displacement setting, you can get a lot of combinations!
- For a displacement enumeration type, if 0 is passed in, it means that no additional operations are done, and the execution efficiency is usually the highest
- If in development, see the enumeration of displacements, and do not do any additional operations, parameters can be directly input 0!
IOS Unique Syntax
- After IOS 5.0, provides a new way to define enumerations
- Specifies the type of data in the enumeration, while defining the enumeration
- typedef ns_options (Nsuinteger, nsjsonreadingoptions)
Displacement enumeration, you can use a bitwise OR SET value
- typedef ns_enum (Nsinteger, Uitableviewstyle)
Numeric enumeration, setting values directly using enumerations
typedef ns_options (Nsuinteger, ActionType) {
Actiontypetop = 1 << 0,
Actiontypebottom = 1 << 1,
Actiontypeleft = 1 << 2,
Actiontyperight = 1 << 3
};
Step 2-bit shift enumeration