typedef NS_OPTIONS(_type, _name) new; -> 位移的,可以使用 按位或 设置数值
typedef NS_ENUM(_type, _name) new; -> 数字的,直接使用枚举设置数值
/*
typedef enum new;
new:枚举类型的变量值列表
C 样式的枚举默认枚举类型变量值的格式为整型
*/
typedef enum {
AA,
BB,
CC
} Name;
- (void)studentWithName:(Name)name {
switch (name) {
case 0:
NSLog(@"AA");
break;
case 1:
NSLog(@"BB");
break;
case 2:
NSLog(@"CC");
break;
default:
break;
}
}
[self studentWithName:1];
[self studentWithName:CC];
/*
typedef NS_ENUM(_type, _name) new;
_type:枚举类型变量值的格式
_name:枚举类型的名字
new:枚举类型的变量值列表
*/
typedef NS_ENUM(NSUInteger, Seasons) {
spring = 0,
summer,
autumn,
winter
};
- (void)selectWithSeasons:(Seasons)seasons {
if (seasons == 1 || seasons == 2) {
NSLog(@"comfortable");
}
else {
NSLog(@"cold");
}
}
[self selectWithSeasons:0];
[self selectWithSeasons:autumn];
/*
typedef NS_OPTIONS(_type, _name) new;
_type:枚举类型变量值的格式
_name:枚举类型的名字
new:枚举类型的变量值列表
位移的枚举判断不能使用 else,否则会丢选项
*/
typedef NS_OPTIONS(NSUInteger, ActionTypeOptions) {
ActionTypeTop = 1 << 0,
ActionTypeBottom = 1 << 1,
ActionTypeLeft = 1 << 2,
ActionTypeRight = 1 << 3
};
- (void)movedWithActionType:(ActionTypeOptions)type {
if (type == 0) {
return;
}
if (type & ActionTypeTop) {
NSLog(@"上 %li", type & ActionTypeTop);
}
if (type & ActionTypeBottom) {
NSLog(@"下 %li", type & ActionTypeBottom);
}
if (type & ActionTypeLeft) {
NSLog(@"左 %li", type & ActionTypeLeft);
}
if (type & ActionTypeRight) {
NSLog(@"右 %li", type & ActionTypeRight);
}
}
[self movedWithActionType:0];
[self movedWithActionType:ActionTypeLeft | ActionTypeTop | ActionTypeBottom | ActionTypeRight];
原文:https://www.cnblogs.com/CH520/p/9508365.html