enum和typedef enum 在IOS中的使用

第一、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....

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章