iOS - OC Enum 枚举

iOS - OC Enum 枚举

前言

  • iOS 5.0 之后,提供了新的枚举定义方式,定义枚举的同时,可以指定枚举中数据的类型。

        typedef NS_OPTIONS(_type, _name) new;   -> 位移的,可以使用 按位或 设置数值
        typedef NS_ENUM(_type, _name) new;      -> 数字的,直接使用枚举设置数值
  • 位移型枚举:

    • 使用 按位或 可以给一个参数同时设置多个 "类型"。在具体执行的时候,使用 按位与 可以判断具体的 "类型"。
    • OC 中 64 位操作系统 NSInteger 64 位 - long => 能够表示 64 种选项。通过位移设置,就能够得到非常多的组合。
    • 对于位移枚举类型,如果传入 0,表示什么附加操作都不做!=> 执行效率是最高的。如果开发中,看到位移的枚举,同时不要做任何的附加操作,参数可以直接输入 0!

1、枚举的定义

1.1 C 样式枚举定义

  • 定义枚举类型

        /*
            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];

1.2 数字型枚举定义

  • 定义枚举类型

        /*
            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];

1.3 位移型枚举定义

  • 定义枚举类型

        /*
            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];
posted @ 2016-08-17 17:42 QianChia 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值