Li Hongqiang iOS Development-usage of enum and typedef enum

Source: Internet
Author: User
Tags button type

Li Hongqiang iOS Development-usage of enum and typedef enum

01-Defining Enum types

We're going to define an enumeration type in ViewController.h, where the value of an enumeration type defaults to a contiguous natural number , for example
to_be_paid=0,//start
Then the next is a ... . So it is generally only necessary to set the first value in the enumeration .

Note: When defining an enumeration type, be sure to define it between #imort and €interface in. h, where the position cannot be wrong.

02-Define Operation type

The use of enum and enum typedef in IOS

First, the use of typedef

The definition of a typedef in C is used to declare a new type name instead of an existing type name, and a typedef is a C keyword that defines a new name for a data type. The data types here include the internal data type (INT,CHAR, etc.) and the custom data type (struct, etc.)

such as: typedef char gender;

Gender A; same as char A; statement.

Second, the use of enum

An enum is an enumeration type, which is used to define a series of macro definition constants, which is equivalent to a series of # define XX xx, and of course the identifiers behind it can also be used as a type identifier.

Such as:

Enum Alerttablesections

{

Kuiaction_simple_section = 1,

Kuiaction_okcancel_section,

Kuiaction_custom_section,

Kuialert_simple_section,

Kuialert_okcancel_section,

Kuialert_custom_section,

};

The value of Kuiaction_okcancel_section is 2.

Third, the use of typedef enum

typedef enum is used to define a data type, then the variable value of that type can only be taken within the scope defined by the enum.

typedef enum {

Uibuttontypecustom = 0, //No button type

Uibuttontyperoundedrect,//rounded rect, flat white button, like in Address Card

Uibuttontypedetaildisclosure,

Uibuttontypeinfolight,

Uibuttontypeinfodark,

Uibuttontypecontactadd,

} Uibuttontype;

Uibuttontype represents a category whose value can only be uibuttontypecustom ....

Before you know the difference between an enum and a typedef enum, you should understand the usage and significance of the typedef.

The typedef in C language is interpreted to declare a new type name in place of the existing class name, for example:

typedef int change;

The change is specified to represent the int type, and the change represents int, then:

int A, B, and change a a A, is equivalent, the same.

Convenient for personal habits, familiar people use change to define int.

A typedef is a C-language keyword that defines a new name for a data type. The data types here include the internal data type (INT,CHAR, etc.) and the custom data type (struct, etc.).

While Enum is an enumeration type, with TypeDef's understanding it is easy to see that typedef enum defines the enumeration type, and the value of the type variable is taken within the range of enum{}, and there is no difference in use.

Enum Alerttablesections

{

Kuiaction_simple_section = 0,

Kuiaction_okcancel_section,

Kuiaction_custom_section,

Kuialert_simple_section,

Kuialert_okcancel_section,

Kuialert_custom_section,

};

typedef enum {

Uibuttontypecustom = 0, //No button type

Uibuttontyperoundedrect,//rounded rect, flat white button, like in Address Card

Uibuttontypedetaildisclosure,

Uibuttontypeinfolight,

Uibuttontypeinfodark,

Uibuttontypecontactadd,

} Uibuttontype;

Look at the above two examples to better understand, the following is the UIButton api,uibuttontype the type of the specified button, clear name, the above directly invoke the element in the enum can be.

Li Hongqiang iOS Development-enum and typedef enum usage

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.