1. Benefits
In the development of the Mall project, when the user purchase is completed, we often push to the corresponding controller according to the type of the goods we buy, and there are many kinds of commodity types, if the use of judgment statements, the code is bloated or difficult to understand, at this time, we can use enumerations to represent the type of goods, Then use the switch statement to perform the corresponding operation, so that the code is much more concise, more conducive to our development;
Moreover, in the Multi-tab, the enumeration to represent the selected items, but also more conducive to our development;
2. Statement
typedef ns_enum (Nsinteger, OrderType) { 0, 10, // shift left 11, 12,};
3, in the state of use
Depending on the type of product selected-(void) Choosegiftwithordertype: (ordertype) type {
Switch(type) { CaseOrdertypebusi: {[self confirmbusi]; } Break; Caseordertypelift: {[self confirmlift]; } Break; Caseordertypegift: {[self confirmgift]; } Break; Caseordertypesafecity: {[self confirmcity]; } Break ; }}
4. Multi-Select operation
// new and old values for monitoring load progress in Wkwebview [_wk addobserver:self Forkeypath:@ "estimatedprogress" | Nskeyvalueobservingoptionold Context:nil];
5, the attention point
1> should use enumerations to represent state machine states, options passed to methods, and status code equivalents, giving these values an understandable name.
2> If the option passed to a method is expressed as an enumeration type, and multiple options can be used simultaneously, then each option value is defined as a power of 2 to be combined by a bitwise OR operation.
3> do not implement the default branch in a switch statement that handles enumeration types. In this case, after adding the new enumeration, the compiler will prompt the developer: The switch statement does not handle all enumerations.
An effective way of programming--using enumerations to represent options and states