52个有效方法(5) - 用枚举表示状态、选项、状态码

枚举(enum)

  • 枚举(enum)只是一种常量的命名方式。是C语言中的一种基本数据类型,是一个"被命名的整型常量"的集合。网络

  • 规范的定义代码中的状态、选项等“常量”。url

  • 不参与内存的占用和释放。code

  • 在开发中使用枚举的目的,是为了增长代码的可读性。对象

NS_ENUMNS_OPTIONS 宏来定义枚举类型,并指明其底层数据类型。

  • NS_ENUMip

    NS_ENUM通常用来定义默认的枚举值内存

/* NS_ENUM supports the use of one or two arguments. The first argument is always the integer type used for the values of the enum. The second argument is an optional type name for the macro. When specifying a type name, you must precede the macro with 'typedef' like so:
 
typedef NS_ENUM(NSInteger, NSComparisonResult) {
    ...
};
 
If you do not specify a type name, do not use 'typedef'. For example:
 
NS_ENUM(NSInteger) {
    ...
};
*/
 
#define NS_ENUM(...) CF_ENUM(__VA_ARGS__)
  • NS_OPTIONSci

    NS_OPTIONS通常用来定义位移相关操做的枚举值开发

    #define NS_OPTIONS(_type, _name) CF_OPTIONS(_type, _name)

枚举(Enum)与状态(states)

某个对象所经历的各类状态能够定义为一个的枚举集(enumeration set)get

typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
    UIViewAnimationTransitionNone,
    UIViewAnimationTransitionFlipFromLeft,
    UIViewAnimationTransitionFlipFromRight,
    UIViewAnimationTransitionCurlUp,
    UIViewAnimationTransitionCurlDown,
};
  • 编译器会为每一个枚举值分配一个独有的编号,从0开始,依次加1编译器

  • 一个字节最多可表示0~255256种(2^8)枚举变量。

枚举(enum)与选项(options)

在定义选项的时候,应该使用枚举类型。若这些选项能够彼此组合,则更应如此。只要枚举定义得对,各选项之间就能够经过 “按位或操做符”来组合。

typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
  • 选项是用位运算的方式定义的
UIViewAutoresizingNone                            0 0 0 0 0 0 0 0
UIViewAutoresizingFlexibleLeftMargin              0 0 0 0 0 0 0 1
UIViewAutoresizingFlexibleWidth                   0 0 0 0 0 0 1 0
UIViewAutoresizingFlexibleRightMargin             0 0 0 0 0 1 0 0
UIViewAutoresizingFlexibleTopMargin               0 0 0 0 1 0 0 0
UIViewAutoresizingFlexibleHeight                  0 0 0 1 0 0 0 0
UIViewAutoresizingFlexibleBottomMargin            0 0 1 0 0 0 0 0
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight 
                                                  0 0 0 1 0 0 1 0
  • <<带符号左移 (n<<2 将整型值带符号左移2位 )
    • 将一个运算符对象的各二进制位所有左移若干位(左边的二进制位丢弃,右边补0

    • 操做数每左移一位,至关于该数乘以2

    • 例如:3<<2 后,结果为12
      3的二进制位11,左移两位后,右边补2个0就是11001100转为10进制为12

    • 左移操做就至关于22次方×3。 每左移1位次方就增1

  • >>带符号右移 (n>>2 将整型值带符号右移2位 )

    • 将一个数的各二进制位所有右移若干位,正数左补0,负数左补1,右边丢弃。

    • 操做数每右移一位,至关于该数除以2 ,而后取整

    • 例如:9>>1 后,结果为4
      9的二进制为1001,右移1位后,左正数补0,右边丢弃。结果为 0100。转为10进制后为4

枚举(enum)与状态码(codes)

枚举用法也可用于 switch 语句。在处理枚举类型的switch语句中不要实现default分支。

typedef NS_ENUM(NSUInteger, EOCConnectionState) {
    EOCConnectionStateDisconnected,
    EOCConnectionStateConnecting,
    EOCConnectionStateConnected
};

switch (_currentState) {
    EOCConnectionStateDisconnected:
      //...
      break;
    EOCConnectionStateConnecting:
      //...
      break;
    EOCConnectionStateConnected:
      //...
      break;
}

要点

  1. 应该用枚举表示状态机的状态、传递给方法的选项以及状态码等值,给这些值起个易懂的名字,就像监听网络状态的枚举。

  2. 若是把传递给某个方法的选项表示为枚举类型,而多个选项又可同时使用,那么就将各选项定义为2的幂,以便经过按位或操做将其组合起来。

  3. NS_ENUMNS_OPTIONS 宏来定义枚举类型,并指明其底层数据类型。这样能够确保枚举是用开发者所选的底层数据类型实现出来的,而不会采用编译器所选的类型。

  4. 在处理枚举类型的switch语句中不要实现default分支。这样的话,加入新枚举以后,编译器就会提示开发者:switch语句并未处理全部枚举。

相关文章
相关标签/搜索