iOS 开发-- enum与typeof enum用法

一, 两者的用法

  枚举类型定义用关键字enum标识,形式为:

enum标识符

{

  枚举数据表

};

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

typedef :typedef为C语言的关键字,作用是为一种数据类型定义一个新名字。这里的数据类型包括内部数据类型(int, char等)和自定义的数据类型(struct等)

enum是枚举类型,有了typedef的理解容易看出,typedef enum定义了枚举类型,类型变量取值在enum{}范围内取,在使用中二者无差别。

例如:

enum HorizontalTitleAlignment

{

    HorizontalTitleAlignmentLeft,

    HorizontalTitleAlignmentCenter,

    HorizontalTitleAlignmentRight

};

HorizontalTitleAlignmentCenter的值为2。

typedef 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...

posted on 2015-12-04 10:37  Fs_purple  阅读(779)  评论(0编辑  收藏  举报