通过在自定义枚举设置位移,在给属性设置或者传递参数的时候,可以根据按位与计算出先前设定了哪些值。
#import "CZViewController.h"
typedef enum {
CZActionTypeTop = 1 << 0,
CZActionTypeLeft = 1 << 1,
CZActionTypeBottom = 1 << 2,
CZActionTypeRight = 1 << 3
} CZActionType;
@interface CZViewController ()
@end
@implementation CZViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%d - %d - %d -%d",CZActionTypeTop,CZActionTypeLeft,CZActionTypeBottom,CZActionTypeRight);
// | 按位或
//type本身就是一个整数,通过按位或的方式,可以设置多个选项
//在oc开发中,如果碰到一个枚举类型是位移的,同时第一个数值不是0的,那么就暗示我们,如果设置为0,表示什么附加操作都不做。
CZActionType type = CZActionTypeTop | CZActionTypeLeft | CZActionTypeBottom;
[self demoType:type];
}
-(void)demoType:(CZActionType)type{
if(type == 0){
NSLog(@"默认");
return;
}
//& 按位与 可以过滤不同的选项
if(type & CZActionTypeTop){
NSLog(@"top");
}
if(type & CZActionTypeLeft){
NSLog(@"left");
}
if(type & CZActionTypeBottom){
NSLog(@"bottom");
}
if(type & CZActionTypeRight){
NSLog(@"right");
}
}