The use of iOS enumeration

Source: Internet
Author: User

1. What is an enumeration?

An enumeration is actually a collection of integral constants, and the simplest example is the one that represents the week sun, MON, TUE, WED, THU, Fri,sat, is an enumeration.

Use of enumerations in 2.iOS

In the OBJC can be enumerated in the way of C language, in fact, OBJC enumeration is in the C language evolved, first understand the C language defines an enumeration way to help us understand the enumeration.

   

Below we define an enumeration type Animal, in the form of enum enum name {enumeration value table}; We declare multiple member names in an enumeration named animal, and we can treat them as aliases for shaping constants. We assign the cat a value of 1 and the system will

The default value for subsequent enumeration members is added 1 on the previous member. Of course, we can also customize the value of the enumeration member, for example, we can assign a value of 50,dog to 100 for cat, all the key to the assignment is to make it easy for programmers to read and understand.

    • Enum: keyword for declaring enumerations
    • Enumeration members and enumeration types with the same name cannot appear in enumerations
// definition of enum type enum animal{    Cat=1, dog, pig, bee, monkey, Elephant, butterfly};

enum Animal Animal = cat; // declaring an enumeration variable of animal enum type

  

We can also declare enum types and enum variables at the same time , and the first way to define them does not define the type of the enumeration, but it declares the enumeration variable workday. The second defines enum-type enum week and variable days, respectively.

// type definition and variable declaration at the same time enum // Unlike the first definition, the type designator here is omitted, which is allowed.  {    Saturday,    0,    Monday,    Tuesday,    Wednesday,    Thursday,     // Variable Workday
enum Week {mon=1// variable days is of enum type enum week

Use the TypeDef keyword to define an enumeration type as an alias and use that alias for variable declaration, in the following example we define the enum car as an alias car.

 typedef enum  car 
{ // The following is an enumeration member BMW = 0 Span style= "COLOR: #000000", Benz, Ferrari, Royce}car; // enumeration name car Car; // declares a variable of car type
enum    // you can omit car from enum car {    // The following are enumeration members    0,    Benz,    Ferrari,    Royce}car; // Enumeration Name
Car  car;//Declaring a variable of car type

 

In iOS, the main Apple recommends using ns_enum,ns_options two ways to declare an enumeration.

    • NS_ENUMDeclaring a generic type, a NS_OPTIONS claim mask (bitmasked) type

The following is a declared enumeration using the Ns_options mask type, in the form of TypeDef ns_options (enumeration member type, enumeration type name) {enumeration member ...};

"<<" is the left-shift operator, and "1 << 0" means 1 left shift 0 bits. In binary, the left shift means that the corresponding decimal number is multiplied by 2.

Benefit: We see in the enumeration values << that we can do by | (bitwise operator: OR)组合!如:下面黄色标注的赋值语句。

//ns_options: is used to declare bitmask (bitmasked)typedef ns_options (nsinteger,mycatevent) {Catnone=0, Catwalk=1 << 0,//<< indicates that the left shift operator represents 0001 catjump=1<<1, Catbark=1<<2, Cateat=1<<3, Catsleep=1<<4, catdaily = Catwalk | catjump};

&Let's take a look at the output of the results such as:

  

We can see that our defined catwalk value is 1,catjump to 2, so or the value of the operation is 3. In the print statement, we use value to perform and operate on all enumeration members. We can judge value by the value of the output

Contains which enumeration members. The result is 0 not included, the result is 1 corresponds to catwalk, and so on.

Declaring a generic type with Ns_enum, the following declares two enumeration types, each of which is assigned a decimal number and a hexadecimal number.

Why use two different ways? In essence, there is no difference between the two approaches, but the number of 16 binary is easier to convert to 2, and when we need to do and or do the enumeration members more convenient.

// Ns_enum: Is an enumeration used to declare generic Nsinteger (the following code uses the Nsinteger) type typedef ns_enum (nsinteger,myboyevent) {    boyfeedcat  0,    boywalkcat    1,};  
// Hexadecimal enumeration typedef ns_enum (nsinteger,dailyevent) {    dailynone       = 0x00,    dailyread        0x01 ,    Dailywrite      0x02,    dailysing       0x03,    dailyshopping    0x04 ,};

If there are any errors above, I hope you correct me.

More iOS notes can come to my blog: tianxianbob.com ac

Reference Link: http://www.cnblogs.com/JCSU/articles/1299051.html

Http://www.jianshu.com/p/97e582fe89f3

The use of iOS enumeration

Related Article

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.