enum和typedef enum 在IOS中的使用

来源:互联网 发布:hadoop超市数据分析 编辑:程序博客网 时间:2024/05/18 00:35

第一、typedef的使用

C语言里typedef的解释是用来声明新的类型名来代替已有的类型名,typedef为C语言的关键字,作用是为一种数据类型定义一个新名字。这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等)

如:typedef  char gender;

gender a;与char a;语句相同。


第二 、enum的使用

enum是枚举类型, enum用来定义一系列宏定义常量区别用,相当于一系列的#define xx xx,当然它后面的标识符也可当作一个类型标识符。

如:

enum AlertTableSections

{

kUIAction_Simple_Section = 1,

kUIAction_OKCancel_Section,

kUIAction_Custom_Section,

kUIAlert_Simple_Section,

kUIAlert_OKCancel_Section,

kUIAlert_Custom_Section,

}; 

kUIAction_OKCancel_Section的值为2.

给枚举类型变量赋值需转换:mytablesection = (enum AlertTableSection)5;

第三、typedef enum 的使用

typedef  enum则是用来定义一个数据类型,那么该类型的变量值只能在enum定义的范围内取。

typedef enum {

    UIButtonTypeCustom = 0,           // no button type

    UIButtonTypeRoundedRect,          // rounded rect, flat white button, like in address card


    UIButtonTypeDetailDisclosure,

    UIButtonTypeInfoLight,

    UIButtonTypeInfoDark,

    UIButtonTypeContactAdd,

} UIButtonType;


UIButtonType表示一个类别,它的值只能是UIButtonTypeCustom....

0 0