1.NS_ENUM NS_OPTIONS
- NS_ENUM NS_OPTIONS 是編譯器定義的用來(lái)方便聲明枚舉的宏
- NSENUM NS_OPTIONS 的宏中對(duì)編譯器的新特性進(jìn)行了判斷
2.一次只能賦一個(gè)值的枚舉變量
定義
typedef NS_ENUM(NSUInteger, State) {
StateConnected,
StateConnecting,
StateFailed
};
使用方式:swtich一定要把所有的情況覆蓋瓮具,而不要用default荧飞,否則以后一旦增加狀態(tài),不會(huì)有提示名党。
State state = StateConnected;
switch (state) {
case StateConnected:
break;
case StateFailed:
break;
case StateConnecting:
break;
}
3.可以同時(shí)賦值多個(gè)值的枚舉類型
定義
typedef NS_OPTIONS(NSUInteger, Direction) {
DirectionUp = 1 << 0,
DirectionDown = 1 << 1,
DirectionLeft = 1 << 2,
DirectionRight
};
使用
Direction direct = DirectionUp | DirectionLeft;
if(direct & DirectionUp){
//執(zhí)行上方向的相應(yīng)代碼
}
if(direct & DirectionDown){
//執(zhí)行下方向的相應(yīng)代碼
}