枚举
- 枚举相信大家都不陌生,其分类有几种
普通枚举
typedef enum {}枚举名称
typedef enum {
ProductTypehaiyun = 1,
ProductTypeAir = 2
}ProductType;
定义类型的枚举
typedef NS_ENUM(NSInteger,名称){}
//定义类型
typedef NS_ENUM(NSInteger,LSLActionType)
{
LSLActionTypeTop = 1,
LSLActionTypeLeft = 2,
};
位移枚举
- 定义
typedef NS_OPTIONS(NSInteger, 名称){}
- 如果枚举,观察第一个值,如果该枚举!=0,那么可以默认传0做参数,如果传0做参数,那么效率最高
typedef NS_OPTIONS(NSInteger, LSLType)
{ //计算左移位数 1*2的n次方
LSLTypeOne = 1 << 0, //1左移0位 1
LSLTypeTwo = 1<<1, //1左移1位 2
LSLTypeThree = 1 <<2, //1左移2位 1*2
LSLTypeFour = 1 << 3
};
这个枚举,经常看到,比如:SDWebImage,系统的枚举等
//定义枚举
typedef NS_OPTIONS(NSInteger, LSLDirectionType)
{ //计算左移位数 1*2的n次方
LSLDirectionTypeTop = 1 << 0, //1左移0位 1
LSLDirectionTypeLeft = 1<<1, //1左移1位 2
LSLDirectionTypeRight = 1 <<2, //1左移2位 1*2
LSLDirectionTypeBottom = 1 << 3
};
- (void)viewDidLoad {
[super viewDidLoad];
[self drawDirectionWithType:LSLDirectionTypeBottom | LSLDirectionTypeTop |LSLDirectionTypeLeft |LSLDirectionTypeRight];
}
- (void)drawDirectionWithType:(LSLDirectionType)dir {
if (dir & LSLDirectionTypeTop) {
NSLog(@"向上:%zd---%zd",LSLDirectionTypeTop,dir);
}
if (dir & LSLDirectionTypeLeft) {
NSLog(@"向左:%zd---%zd",LSLDirectionTypeLeft,dir);
}
if (dir & LSLDirectionTypeRight) {
NSLog(@"向右、:%zd---%zd",LSLDirectionTypeRight,dir);
}
if (dir & LSLDirectionTypeBottom) {
NSLog(@"向下:%zd---%zd",LSLDirectionTypeBottom,dir);
}
}