列舉(enum)
-
列舉(enum)只是一種常量的命名方式,是C語言中的一種基本資料型別,是一個"被命名的整型常量"的集合,
-
規范的定義代碼中的狀態、選項等“常量”,
-
不參與記憶體的占用和釋放,
-
在開發中使用列舉的目的,是為了增加代碼的可讀性,
用 NS_ENUM 與 NS_OPTIONS 宏來定義列舉型別,并指明其底層資料型別,
-
NS_ENUMNS_ENUM一般用來定義默認的列舉值
/* NS_ENUM supports the use of one or two arguments. The first argument is always the integer type used for the values of the enum. The second argument is an optional type name for the macro. When specifying a type name, you must precede the macro with 'typedef' like so:
typedef NS_ENUM(NSInteger, NSComparisonResult) {
...
};
If you do not specify a type name, do not use 'typedef'. For example:
NS_ENUM(NSInteger) {
...
};
*/
#define NS_ENUM(...) CF_ENUM(__VA_ARGS__)
-
NS_OPTIONSNS_OPTIONS一般用來定義位移相關操作的列舉值
#define NS_OPTIONS(_type, _name) CF_OPTIONS(_type, _name)
列舉(Enum)與狀態(states)
某個物件所經歷的各種狀態可以定義為一個的列舉集(enumeration set)
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
};
-
編譯器會為每個列舉值分配一個獨有的編號,從
0開始,依次加1, -
一個位元組最多可表示
0~255共256種(2^8)列舉變數,
列舉(enum)與選項(options)
在定義選項的時候,應該使用列舉型別,若這些選項可以彼此組合,則更應如此,只要列舉定義得對,各選項之間就可以通過 “按位或運算子”來組合,
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
- 選項是用位運算的方式定義的
UIViewAutoresizingNone 0 0 0 0 0 0 0 0
UIViewAutoresizingFlexibleLeftMargin 0 0 0 0 0 0 0 1
UIViewAutoresizingFlexibleWidth 0 0 0 0 0 0 1 0
UIViewAutoresizingFlexibleRightMargin 0 0 0 0 0 1 0 0
UIViewAutoresizingFlexibleTopMargin 0 0 0 0 1 0 0 0
UIViewAutoresizingFlexibleHeight 0 0 0 1 0 0 0 0
UIViewAutoresizingFlexibleBottomMargin 0 0 1 0 0 0 0 0
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
0 0 0 1 0 0 1 0
-
<<帶符號左移 (
n<<2將整型值帶符號左移2位 )-
將一個運算子物件的各二進制位全部左移若干位(左邊的二進制位丟棄,右邊補
0) -
運算元每左移一位,相當于該數乘以
2 -
例如:
3<<2后,結果為12
3的二進制位11,左移兩位后,右邊補2個0就是1100,1100轉為10進制為12, -
左移操作就相當于
2的2次方×3, 每左移1位次方就增1
-
-
>>帶符號右移 (
n>>2將整型值帶符號右移2位 )-
將一個數的各二進制位全部右移若干位,正數左補
0,負數左補1,右邊丟棄, -
運算元每右移一位,相當于該數除以
2,然后取整 -
例如:
9>>1后,結果為4
9的二進制為1001,右移1位后,左正數補0,右邊丟棄,結果為0100,轉為10進制后為4,
-
列舉(enum)與狀態碼(codes)
列舉用法也可用于 switch 陳述句,在處理列舉型別的switch陳述句中不要實作default分支,
typedef NS_ENUM(NSUInteger, EOCConnectionState) {
EOCConnectionStateDisconnected,
EOCConnectionStateConnecting,
EOCConnectionStateConnected
};
switch (_currentState) {
EOCConnectionStateDisconnected:
//...
break;
EOCConnectionStateConnecting:
//...
break;
EOCConnectionStateConnected:
//...
break;
}
要點
-
應該用列舉表示狀態機的狀態、傳遞給方法的選項以及狀態碼等值,給這些值起個易懂的名字,就像監聽網路狀態的列舉,
-
如果把傳遞給某個方法的選項表示為列舉型別,而多個選項又可同時使用,那么就將各選項定義為2的冪,以便通過按位或操作將其組合起來,
-
用
NS_ENUM與NS_OPTIONS宏來定義列舉型別,并指明其底層資料型別,這樣可以確保列舉是用開發者所選的底層資料型別實作出來的,而不會采用編譯器所選的型別, -
在處理列舉型別的
switch陳述句中不要實作default分支,這樣的話,加入新列舉之后,編譯器就會提示開發者:switch陳述句并未處理所有列舉,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/11422.html
標籤:iOS
