Original Http://tutuge.me/2015/03/21/effective-objective-c-5-enum/?utm_source=tuicool&utm_medium=referral Preface
Enums, which are enumerations, start with the C language, C + +, Java, objective-c, Swift, all of which have corresponding enumeration types, and may have less functionality, but the core is a--the "constant" of States, options, and so on in the definition code of the specification.
Item 5-use enumerations for states, Options, and Status Codes
The content of this section is how to use enumerations correctly.
The difference between State and option (States and options)
Before using enum , I personally felt that it was necessary to differentiate between the concepts of State and option.
state , at the same time there can only be one, such as "OK", "error", can not be both OK and error.
options , and can have one or more, such as the app can support both horizontal and vertical screen, horizontal screen vertical screen at this time is "screen orientation" two different options.
Next, let's look at how to define states and options with enumerations.
Enum and State (states) bad practices
Often see this way of writing:
1 #define STATE_OK 02#define state_error 13#define state_unknow 245 receive 6int state = State_unknow directly with int type variable ;
This is done with the following "inappropriate":
- A macro definition has no type constraints, just a simple substitution.
- In all cases where the status cannot be restricted, for example, if the state is assigned a value of 3, the program may be faulted and the matching state cannot be found because the compiler does not have a "status= 3;" A warning is provided.
The right approach
1 enum _ttgstate {2 Ttgstateok 0,3 ttgstateerror, 4 Ttgstateunknow5} ttgstate; 6 7 // indicates enumeration type 8 ttgstate state = Ttgstateok;
The use of the time is as follows:
1- (void) Dealwithstate: (ttgstate) state {2 Switch(state) {3 CaseTtgstateok:4 //...5 Break;6 CaseTtgstateerror:7 //...8 Break;9 CaseTtgstateunknow:Ten //... One Break; A } -}
Enum and option (options)
option, that is, the type of an option variable to be able to represent the selection of one or more combinations, as in the following example:
1 //direction, can support one or more directions at the same time2typedefenum_ttgdirection {3Ttgdirectionnone =0,4Ttgdirectiontop =1<<0,5Ttgdirectionleft =1<<1,6Ttgdirectionright =1<<2,7Ttgdirectionbottom =1<<38} ttgdirection;
See, the options here are defined in the way of bitwise operations , and the advantage is that our option variables can be represented as follows:
1 //assigning multiple options at the same time with the OR Operation2Ttgdirection Direction = Ttgdirectiontop | Ttgdirectionleft |Ttgdirectionbottom;3 4 //use the "and" operation to remove the corresponding bit5 if(Direction &ttgdirectiontop) {6NSLog (@"Top");7 }8 if(Direction &ttgdirectionleft) {9NSLog (@" Left");Ten } One if(Direction &ttgdirectionright) { ANSLog (@" Right"); - } - if(Direction &Ttgdirectionbottom) { theNSLog (@"Bottom"); -}
The actual memory of the direction variable is as follows:
This allows multiple values to be supported at the same time using bitwise operations.
Enum in Objective-c "upgraded version"
In general, we cannot specify what the actual type of the enumeration variable is, that is, we do not know whether the enumeration is finally int, or what type it is. But starting with C + + 11, we can specify its actual storage type for enumerations, such as the following syntax:
enum Ttgstate:nsinteger {/*... */};
But how do we ensure compatibility when we define enumerations? The foundation framework has provided us with a more "uniform, convenient" enumeration definition method, and we redefine the example above:
1 //Ns_enum, defining normal enumerations such as State2 typedef ns_enum (Nsuinteger, ttgstate) {3Ttgstateok =0,4 Ttgstateerror,5 Ttgstateunknow6 };7 8 //ns_options, defining options9 typedef ns_options (Nsuinteger, ttgdirection) {TenTtgdirectionnone =0, OneTtgdirectiontop =1<<0, ATtgdirectionleft =1<<1, -Ttgdirectionright =1<<2, -Ttgdirectionbottom =1<<3 the};
Therefore, in the development of Mac, iOS programs, it is best to use the "ns_enum" and "ns_options" definitions, to ensure unity.
Summarize
Full use of the enumeration, can enhance the readability of the code, reduce various "errors", so that the code more specification.
Correct use of enum-enumeration-effective-objective-c-reading notes