• 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、C 样式枚举定义

    • 1.1 定义枚举类型

      /*
      typedef enum new;
      new:枚举类型的变量值列表
      C 样式的枚举默认枚举类型变量值的格式为整型
      */
      typedef enum {
      	AA,
      	BB,
      	CC
      } Name;
      
    • 1.2 判断枚举值

      - (void)studentWithName:(Name)name {
          switch (name) {
              case 0:
                  NSLog(@"AA");
                  break;
              case 1:
                  NSLog(@"BB");
                  break;
              case 2:
                  NSLog(@"CC");
                  break;
              default:
                  break;
          }
      }
      
    • 1.3 设置枚举的值

      [self studentWithName:1];
      [self studentWithName:CC];
      

    2、数字型枚举定义

    • 2.1 定义枚举类型

      /*
          typedef NS_ENUM(_type, _name) new;                      
          _type:枚举类型变量值的格式
          _name:枚举类型的名字
          new:枚举类型的变量值列表
      */
      
      typedef NS_ENUM(NSUInteger, Seasons) {
      
          spring = 0,
          summer,
          autumn,
          winter
      };
      
    • 2.2 判断枚举值

      - (void)selectWithSeasons:(Seasons)seasons {
      
          if (seasons == 1 || seasons == 2) {
              NSLog(@"comfortable");
          } 
          else {
              NSLog(@"cold");
          }
      }
      
    • 2.3 设置枚举的值

      [self selectWithSeasons:0];
      [self selectWithSeasons:autumn];
      

    3、位移型枚举定义

    • 3.1 定义枚举类型

      /*
          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
      };
      
    • 3.2 判断枚举值

      - (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);
      	}
      }
      
    • 3.3 设置枚举的值

      [self movedWithActionType:0];
      
      [self movedWithActionType:ActionTypeLeft | ActionTypeTop | ActionTypeBottom | ActionTypeRight];
      
  • 相关阅读:
    node读写文件
    idea下建立bootstrap项目
    webpack
    Bootstrap-javascript插件
    Bootstrap-other内置组件
    Centos 修改当前路径显示为全路径
    深入理解java虚拟机(4)类加载的过程
    深入理解java虚拟机(3)垃圾收集器与内存分配策略
    深入理解java虚拟机(2)
    Scala学习笔记(3)
  • 原文地址:https://www.cnblogs.com/CH520/p/9508365.html
Copyright © 2020-2023  润新知