位移枚举解析

位移枚举

  • 位移枚举是非常古老的 C 语言技巧

  • 按位与 如果都是 1 结果就是1

  • 按位或 如果都是 0 结果就是0

演练

  • 定义枚举类型

/// 操作类型枚举typedef enum {
    ActionTypeTop       = 1 << 0,
    ActionTypeBottom    = 1 << 1,
    ActionTypeLeft      = 1 << 2,
    ActionTypeRight     = 1 << 3} ActionType;
  • 方法目标

    • 根据操作类型参数,做出不同的响应

    • 操作类型可以任意组合

  • 方法实现

- (void)action:(ActionType)type {    
if (type == 0) {       
 NSLog(@"无操作");        
 return;
    }   
     if (type & ActionTypeTop) {    
       
       NSLog(@"Top %tu", type & ActionTypeTop);
       
    }    if (type & ActionTypeBottom) {  
        
      NSLog(@"Bottom %tu", type & ActionTypeBottom);
      
    }    if (type & ActionTypeLeft) {    
       
     NSLog(@"Left %tu", type & ActionTypeLeft);
     
    }    if (type & ActionTypeRight) {    
     
       NSLog(@"Right %tu", type & ActionTypeRight);
    }
}
  • 方法调用

ActionType type = ActionTypeTop | ActionTypeRight;
[self action:type];

代码小结

  • 使用 按位或 可以给一个参数同时设置多个 类型

  • 在具体执行时,使用 按位与 可以判断具体的 类型

  • 通过位移设置,就能够得到非常多的组合!

  • 对于位移枚举类型,如果传入 0,表示什么附加操作都不做,通常执行效率是最高的

  • 如果开发中,看到位移的枚举,同时不要做任何的附加操作,参数可以直接输入 0!

iOS 特有语法

  • iOS 5.0之后,提供了新的枚举定义方式

  • 定义枚举的同时,指定枚举中数据的类型

  • typedef NS_OPTIONS(NSUInteger, NSJSONReadingOptions)

    • 位移枚举,可以使用 按位或 设置数值

  • typedef NS_ENUM(NSInteger, UITableViewStyle)

    • 数字枚举,直接使用枚举设置数值


typedef NS_OPTIONS(NSUInteger, ActionType) {
    ActionTypeTop       = 1 << 0,
    ActionTypeBottom    = 1 << 1,
    ActionTypeLeft      = 1 << 2,
    ActionTypeRight     = 1 << 3
};





/*******位移枚举演练**********/

//按位与 同1为1 其他为0

//按位或 有1为1 其他为0

//typedef enum{

//

//    ActionUp =    1 << 0,

//    ActionDown =  1 << 1,

//    ActionLeft =  1 << 2,

//    ActionRight = 1 << 3,

//

//

//

//}ActionEnum;

//typedef NS_OPTIONS(NSInteger, Action){

//    

//    ActionUp =    1 << 0,

//    ActionDown =  1 << 1,

//    ActionLeft =  1 << 2,

//    ActionRight = 1 << 3,

//    

//    

//    

//} ;

//

typedef NS_ENUM(NSInteger,ActionEnum){

    ActionUp =    1 << 0,

    ActionDown =  1 << 1,

    ActionLeft =  1 << 2,

    ActionRight = 1 << 3,

};

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];    

    //按位或

    [self didSelcted:ActionUp | ActionDown];

    // 0 0 0 1

    // 0 0 1 0

    // 0 0 1 1 -- 3

}

- (void) didSelcted:(ActionEnum)action{

    // action 0 0 1 1

    // up     0 0 0 1

    //        0 0 0 1 ---> 1

    if ((action & ActionUp)    == ActionUp) {

        NSLog(@"ActionUp %ld",action);

    }

    // action 0 0 1 1

    // up     0 0 1 0

    //        0 0 1 0 ---> 2

    if ((action & ActionDown)  == ActionDown) {

        NSLog(@" ActionDown%ld",action);

    }

    // action 0 0 1 1

    // up     0 1 0 0

    //        0 0 0 1 ---> 0

    if ((action &ActionLeft )  == ActionLeft) {

        NSLog(@"ActionLeft %ld",action);

    }

    // action 0 0 1 1

    // up     1 0 0 0

    //        0 0 0 0 ---> 0

    if ((action & ActionRight) == ActionRight) {

        NSLog(@"ActionRight %ld",action);

    }

}


转载于:https://my.oschina.net/venn0126/blog/543034

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值