真理惟一可靠的標(biāo)準(zhǔn)就是永遠(yuǎn)自相符合柠逞。
枚舉的作用和定義
枚舉的作用在于規(guī)范并語義化的定義代碼中的狀態(tài)、選項(xiàng)等常量景馁。如果一個(gè)變量只有幾種可能的值板壮,比如屏幕支持的方向只有上(Orientation Portrait)、下(Orientation Upside down)合住、左(Orientation Landscape Left)绰精、右(Orientation Landscape Right)。就可以使用枚舉類型來定義一個(gè)代表方向枚舉類型變量透葛。
枚舉類型的定義以關(guān)鍵字enum開頭笨使,之后是枚舉數(shù)據(jù)類型的名稱,最后是在一對(duì)花括號(hào)內(nèi)的選項(xiàng)標(biāo)識(shí)符序列僚害。 (實(shí)現(xiàn)枚舉所用的數(shù)據(jù)類型取決于編譯器硫椰。除了使用默認(rèn)類型,還可以指明用何種“底層數(shù)據(jù)類型”來保存枚舉類型的變量):
//使用編譯器默認(rèn)類型
enum Orientation {
Orientation_Portrait,
Orientation_Down,
Orientation_left,
Orientation_Right
};
//指明枚舉類型保存變量的類型
enum Orientation: NSUInteger {
Orientation_Portrait,
Orientation_Down,
Orientation_left,
Orientation_Right
};
//定義匿名枚舉類型萨蚕,并定義兩個(gè)枚舉變量
enum {
Orientation_Portrait,
Orientation_Down,
Orientation_left,
Orientation_Right
} WX_Orientation,QQ_Orientation;
枚舉類型值的分配原則:
編譯器會(huì)為枚舉分配一個(gè)獨(dú)有的編號(hào)(枚舉變量的本質(zhì)就是無符號(hào)整數(shù)(%u占位符))靶草,從0開始,每個(gè)枚舉遞增1岳遥。(可以手動(dòng)指定某個(gè)枚舉成員所對(duì)應(yīng)的值奕翔,接下來的枚舉值會(huì)在上一個(gè)的基礎(chǔ)上遞增1)。
使用關(guān)鍵字typedef重新定義枚舉寒随,目的是為了簡化枚舉的聲明糠悯,不需要每次都寫enum
enum Orientation: NSUInteger {
Orientation_Portrait,
Orientation_Down,
Orientation_left,
Orientation_Right
};
typedef enum Orientation Orientation;
//使用Orientation代替完整的enum Orientation來聲明兩個(gè)枚舉變量
Orientation ori_1, ori_2;
合并后
typedef enum Orientation: NSUInteger {
Orientation_Portrait,
Orientation_Down,
Orientation_left,
Orientation_Right
} Orientation;
基本使用方法
//定義枚舉
typedef enum Orientation: NSUInteger {
Orientation_Portrait,
Orientation_Down,
Orientation_left,
Orientation_Right
} Orientation;
//創(chuàng)建枚舉變量并賦值
Orientation QQ_Orientation = Orientation_Portrait;
//調(diào)用函數(shù)
[self defineQQOrientation:QQ_Orientation];
- (void)defineQQOrientation:(Orientation)orientation{
switch (orientation) {
case Orientation_Portrait:
break;
case Orientation_Down:
break;
case Orientation_left:
break;
case Orientation_Right:
break;
default:
break;
}
}
枚舉的另一種使用方式是定義為按位掩碼,當(dāng)定義選項(xiàng)的時(shí)候妻往,若這些選項(xiàng)可以彼此組合互艾,則在設(shè)置特定的枚舉值后,各選項(xiàng)間就可通過“按位或”來組合讯泣。因?yàn)槊總€(gè)枚舉值所對(duì)應(yīng)的二進(jìn)制表示中纫普,只有1個(gè)二進(jìn)制位的值是1,所以多個(gè)選項(xiàng)“按位或”后的組合值是唯一的好渠,且將某一選項(xiàng)與組合值做“按位與”操作昨稼,即可判斷出組合值中是否包含該選項(xiàng)。
enum Orientation {
Orientation_Portrait = 1 << 0, // 0001
Orientation_Down = 1 << 1,// 0010
Orientation_left = 1 << 2,// 0100
Orientation_Right = 1 << 3// 1000
};
多選項(xiàng)使用方法:
typedef enum OrientationGroup {
OrientationGroup_Portrait = 1 << 0, // 0001,
OrientationGroup_Down = 1 << 1, // 0010,
OrientationGroup_left = 1 << 2, // 0100,
OrientationGroup_Right = 1 << 3, // 1000
} OrientationGroup;
//用“或”運(yùn)算同時(shí)賦值多個(gè)選項(xiàng)
OrientationGroup orientationGroup = OrientationGroup_left | OrientationGroup_Down;
//用“與”運(yùn)算取出對(duì)應(yīng)位
if (orientationGroup & OrientationGroup_Portrait) {
NSLog(@"OrientationGroup_Portrait OK");
}
if (orientationGroup & OrientationGroup_left) {
NSLog(@"OrientationGroup_left OK");
}
if (orientationGroup & OrientationGroup_Right) {
NSLog(@"OrientationGroup_Right OK");
}
if (orientationGroup & OrientationGroup_Down) {
NSLog(@"OrientationGroup_Down OK");
}
運(yùn)行結(jié)果:
2019-09-19 14:17:52.610619+0800 Text[2143:121039] OrientationGroup_left OK
2019-09-19 14:17:52.610763+0800 Text[2143:121039] OrientationGroup_Down OK
enum在Objective-C中的“升級(jí)版”
從C++ 11開始拳锚,我們可以為枚舉指定其實(shí)際的存儲(chǔ)類型假栓,在枚舉的定義中已經(jīng)提到過,如下
//指明枚舉類型保存變量的類型
enum Orientation: NSUInteger {
Orientation_Portrait,
Orientation_Down,
Orientation_left,
Orientation_Right
};
在Objective-C中為了保證枚舉類型的兼容性霍掺,推薦使用NS_ENUM和NS_OPTIONS
// NS_ENUM匾荆,定義狀態(tài)等普通枚舉類型
typedef NS_ENUM(NSInteger, NS_Orientation) {
NS_Orientation_Portrait,
NS_Orientation_Down,
NS_Orientation_left,
NS_Orientation_Right
};
// NS_OPTIONS拌蜘,定義可組合選項(xiàng)的枚舉類型
typedef NS_OPTIONS(NSInteger, OP_Orientation) {
OP_Orientation_None = 0,
OP_Orientation_Portrait = 1 << 0,
OP_Orientation_Down = 1 << 1,
OP_Orientation_left = 1 << 2,
OP_Orientation_Right = 1 << 3
};