SDWebImage源码阅读前的准备(二)NS_OPTIONS/NS_ENUM

 

原文地址:http://www.jianshu.com/p/30f76a950604

NSObjcRuntime.h 里面的定义:

 1 /* 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:
 2  
 3 typedef NS_ENUM(NSInteger, NSComparisonResult) {
 4     ...
 5 };
 6  
 7 If you do not specify a type name, do not use 'typedef'. For example:
 8  
 9 NS_ENUM(NSInteger) {
10     ...
11 };
12 */
13 #define NS_ENUM(...) CF_ENUM(__VA_ARGS__)
14 #define NS_OPTIONS(_type, _name) CF_OPTIONS(_type, _name)

NS_OPTIONS 实例:

表示轻扫手势的方向:

1  typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
2      UISwipeGestureRecognizerDirectionRight = 1 << 0,  // 值为2的0次方
3      UISwipeGestureRecognizerDirectionLeft  = 1 << 1, // 值为2的1次方
4      UISwipeGestureRecognizerDirectionUp    = 1 << 2, // 值为2的2次方
5      UISwipeGestureRecognizerDirectionDown  = 1 << 3 // 值为2的3次方
6  };

  小括号中第一个为NSUInteger这个为固定值,第二个为枚举类型,自己定义,大括号中枚举项必须全部包含小括号的枚举类型,枚举项后面再跟上几个值的区别,这里枚举项是NSUInteger类型,它的值我已经标记了,看上面注释,当然也可以像下方这样写枚举,但是官方推荐格式为上面那种。

1 typedef enum {
2     UISwipeGestureRecognizerDirectionRight = 1 << 0,  //值为2的0次方
3     UISwipeGestureRecognizerDirectionLeft = 1 << 1,  //值为2的1次方
4     UISwipeGestureRecognizerDirectionUp = 1 << 2,  //值为2的2次方
5     UISwipeGestureRecognizerDirectionDown = 1 << 3  //值为2的3次方
6 }UISwipeGestureRecognizerDirection;

NS_ENUM 实例:

1 /* Values for NSWritingDirection */
2 typedef NS_ENUM(NSInteger, NSWritingDirection) {
3     NSWritingDirectionNatural       = -1,    // Determines direction using the Unicode Bidi Algorithm rules P2 and P3 // 值为 -1
4     NSWritingDirectionLeftToRight   =  0,    // Left to right writing direction // 值为0
5     NSWritingDirectionRightToLeft   =  1     // Right to left writing direction // 值为1
6 } NS_ENUM_AVAILABLE_IOS(6_0);

  小括号中第一个为NSInteger这个为固定值,第二个为枚举类型,自己定义,大括号中枚举项必须包含小括号中自己定义的枚举类型,枚举项自己加后缀以视区别,大括号中的枚举项的值可自定义,若是定义了枚举项其中一项的值后面依次在它的前一项的值上加1,如这样:

 1 typedef NS_ENUM(NSInteger, NSWritingDirection) {
 2     NSWritingDirectionNatural = 0,  //值为0    
 3     NSWritingDirectionLeftToRight,  //值为1
 4     NSWritingDirectionRightToLeft  //值为2       
 5 };
 6 //或者这样
 7 typedef NS_ENUM(NSInteger, NSWritingDirection) {
 8     NSWritingDirectionNatural = 0,  //值为0    
 9     NSWritingDirectionLeftToRight = 2,  //值为2
10     NSWritingDirectionRightToLeft  //值为3       
11 };
12 //若是都不定义值,默认第一项为0,后面依次枚举项的值加1。

  当然也可以下方这样写枚举,但是官方不推荐,还是上面格式规范:

1 typedef enum {
2     NSWritingDirectionNatural = -1,  //值为-1    
3     NSWritingDirectionLeftToRight = 0,  //值为0
4     NSWritingDirectionRightToLeft = 1  //值为1  
5 }NSWritingDirection;

NS_ENUM与NS_OPTIONS区别:

  • NS_ENUM枚举项的值为NSInteger,NS_OPTIONS枚举项的值为NSUInteger
  • NS_ENUM定义通用枚举,NS_OPTIONS定义位移枚举

  位移枚举即是在你需要的地方可以同时存在多个枚举值如这样:

1 UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc] init];
2 swipeGR.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
3 //这里几个枚举项同时存在表示它的方向同时包含1.向下2.向左3.向右

  而NS_ENUM定义的枚举不能几个枚举项同时存在,只能选择其中一项,像这样:

1 NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
2 paragraph.baseWritingDirection = NSWritingDirectionNatural;
  • NS_OPTIONS的枚举项的值需要像这样表示1 << 0,1 << 1,2的几次方这样,而NS_ENUM可以直接给像1,2,3这样。

  END

 

posted @ 2017-04-21 11:13  鳄鱼不怕牙医不怕  阅读(280)  评论(0编辑  收藏  举报