枚举的其中一种方式:位移枚举,直接上代码,看完基本就懂了
// 位移枚举
typedef NS_OPTIONS(NSUInteger, ActionType) {
// 二进制 十进制
ActionTypeUp = 1 << 0, // 0000 0001 1 二进制位左移0位
ActionTypeDown = 1 << 1, // 0000 0010 2 二进制位左移1位
ActionTypeRight = 1 << 2, // 0000 0100 4 二进制位左移2位
ActionTypeLeft = 1 << 3, // 0000 1000 8 二进制位左移3位
};
写一个方法:
-(void)action:(ActionType)type
{
if (type == 0) {
return;
}
if ((type & ActionTypeUp) == ActionTypeUp) {
NSLog(@"上");
}
if ((type & ActionTypeDown) == ActionTypeDown) {
NSLog(@"下");
}
if ((type & ActionTypeLeft) == ActionTypeLeft) {
NSLog(@"左");
}
if ((type & ActionTypeRight) == ActionTypeRight) {
NSLog(@"右");
}
}
在viewDidLoad方法中调用本方法:
ActionType type = ActionTypeUp |ActionTypeDown | 1<<3 | 4; // 按位或运算: 0000 0001 | 0000 0010 | 0000 0100 | 0000 1000,因此得到十进制的值是15
// ActionType type = 15; // 上面一句的代码等同于这一句
[self action:type];
网友评论