在了解enum和typedef enum的區別之前先應該明白typedef的用法和意義。
C語言里typedef的解釋是用來聲明新的類型名來代替已有的類姓名,例如:
typedef int CHANGE;
指定了用CHANGE代表int類型,CHANGE代表int,那么:
int a,b;和CHANGE a,b;是等價的、一樣的。
方便了個人習慣,熟悉的人用CHANGE來定義int。
typedef為C語言的關鍵字,作用是為一種數據類型定義一個新名字。這里的數據類型包括內部數據類型(int,char等)和自定義的數據類型(struct等)。
而enum是枚舉類型,有了typedef的理解容易看出,typedef enum定義了枚舉類型,類型變量取值在enum{}范圍內取,在使用中二者無差別。
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;
看上面兩個例子更好理解,下面的是UIButton的API,UIButtonType指定的按鈕的類型,上面的直接調用enum里的元素就可以了