An effective way of programming--using enumerations to represent options and states

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.