枚举enum、NS_ENUM 、NS_OPTIONS

enum

了解位移枚举之前,我们先回顾一下C语言位运算符。
1     << : 左移,比如1<<n,表示1往左移n位,即数值大小2的n次方; 例如 : 0b0001 <<  1  变为了 0b0010
2     >>  : 右移,类似左移,数值大小除以2的n次方
3     &  : 按位与,1与任意数等于任意数本身,0与任意数等于0,即1&x=x, 0&x= 0
4     |  : 按位或,x| y中只要有一个1则结果为1;反之为0
5     ^  : 按位异或,x^y相等则为0,不等则为1


typedef enum A {
    a =
0 ,
    b,
    c,
    d,
} englishWord;

typedef enum {
    e =
4 ,
    f,
    g,
} englishWord2;

englishWord eg1 = a ;
englishWord2 eg2 = e ;

// enum newNum:NSInteger 枚举变量,并且继承 NSInteger;englishWord3 枚举的别名
typedef enum newNum :NSInteger englishWord3;
enum newNum:NSInteger {
    new1 =
10 ,
    new2,
};
englishWord3 eg3 = new1 ;
eg1 = 0,eg2 = 4, eg3 = 10

2、NS_ENUM 、NS_OPTIONS

OC中常见的枚举,例如常见的:

typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {

UIViewAnimationCurveEaseInOut,         // slow at beginning and end

UIViewAnimationCurveEaseIn,            // slow at beginning

UIViewAnimationCurveEaseOut,           // slow at end

UIViewAnimationCurveLinear

};

typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {

UIViewAnimationOptionLayoutSubviews            = 1 <<  0,

UIViewAnimationOptionAllowUserInteraction      = 1 <<  1, // turn on user interaction while animating

UIViewAnimationOptionBeginFromCurrentState     = 1 <<  2, // start all views from current value, not initial value

UIViewAnimationOptionRepeat                    = 1 <<  3, // repeat animation indefinitely

UIViewAnimationOptionAutoreverse               = 1 <<  4, // if repeat, run animation back and forth

UIViewAnimationOptionOverrideInheritedDuration = 1 <<  5, // ignore nested duration

UIViewAnimationOptionOverrideInheritedCurve    = 1 <<  6, // ignore nested curve

UIViewAnimationOptionAllowAnimatedContent      = 1 <<  7, // animate contents (applies to transitions only)

UIViewAnimationOptionShowHideTransitionViews   = 1 <<  8, // flip to/from hidden state instead of adding/removing

UIViewAnimationOptionOverrideInheritedOptions  = 1 <<  9, // do not inherit any options or animation type

UIViewAnimationOptionCurveEaseInOut            = 0 << 16, // default

UIViewAnimationOptionCurveEaseIn               = 1 << 16,

UIViewAnimationOptionCurveEaseOut              = 2 << 16,

UIViewAnimationOptionCurveLinear               = 3 << 16,

UIViewAnimationOptionTransitionNone            = 0 << 20, // default

UIViewAnimationOptionTransitionFlipFromLeft    = 1 << 20,

UIViewAnimationOptionTransitionFlipFromRight   = 2 << 20,

UIViewAnimationOptionTransitionCurlUp          = 3 << 20,

UIViewAnimationOptionTransitionCurlDown        = 4 << 20,

UIViewAnimationOptionTransitionCrossDissolve   = 5 << 20,

UIViewAnimationOptionTransitionFlipFromTop     = 6 << 20,

UIViewAnimationOptionTransitionFlipFromBottom  = 7 << 20,

} NS_ENUM_AVAILABLE_IOS(4_0);

 

这两个宏的定义在Foundation.framework的NSObjCRuntime.h中:

#if (__cplusplus && __cplusplus >= 201103L && (__has_extension(cxx_strong_enums) || __has_feature(objc_fixed_enum))) || (!__cplusplus && __has_feature(objc_fixed_enum))

#define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type

#if (__cplusplus)

#define NS_OPTIONS(_type, _name) _type _name; enum : _type

#else

#define NS_OPTIONS(_type, _name) enum _name : _type _name; enum _name : _type

#endif

#else

#define NS_ENUM(_type, _name) _type _name; enum

#define NS_OPTIONS(_type, _name) _type _name; enum

#endif


 
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
展开得到:
  1. typedef enum UIViewAnimationTransition : NSInteger UIViewAnimationTransition;  
  2. enum UIViewAnimationTransition : NSInteger {

其实从枚举定义来看,NS_ENUM和NS_OPTIONS本质是一样的,仅仅从字面上来区分其用途。NS_ENUM是通用情况,NS_OPTIONS一般用来定义具有位移操作或特点的情况(bitmask)。

开发中,你也许见到过或用过类似这种的枚举类型:
typedef NS_OPTIONS(NSUInteger, BDRequestOptions) {
    BDRequestOptionSuccess     1 <<  0 ,
    BDRequestOptionFailure     
1 <<  1 ,
    BDRequestOptionProcessing 
1 <<  2 ,
    BDRequestOptionAnimate     
1 <<  3 ,
};

其实这种的并不是枚举,而是按位掩码(bitmask),他的语法和枚举相同。但用法却不同。

示例:

// 首先定义一组
typedef NS_OPTIONS(NSUInteger, BDRequestOptions) {
    BDRequestOptionSuccess     = 1 << 0,
    BDRequestOptionFailure     = 1 << 1,
    BDRequestOptionProcessing  = 1 << 2,
    BDRequestOptionAnimate     = 1 << 3,
};

// 然后调用我们定义的方法
#pragma mark - View lifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    
    [self test:BDRequestOptionSuccess | BDRequestOptionFailure | BDRequestOptionProcessing | BDRequestOptionAnimate];
}

- (void)test:(BDRequestOptions)type {
    if (type & BDRequestOptionSuccess) {
        NSLog(@"BDRequestOptionSuccess");
    }
    if (type & BDRequestOptionFailure) {
        NSLog(@"BDRequestOptionFailure");
    }
    if (type & BDRequestOptionProcessing) {
        NSLog(@"BDRequestOptionProcessing");
    }
    if (type & BDRequestOptionAnimate) {
        NSLog(@"BDRequestOptionAnimate");
    }
}

// 查看打印结果:
2016-04-04 14:09:44.946 OC测试[5869:719056] BDRequestOptionSuccess
2016-04-04 14:09:44.947 OC测试[5869:719056] BDRequestOptionFailure
2016-04-04 14:09:44.947 OC测试[5869:719056] BDRequestOptionProcessing
2016-04-04 14:09:44.947 OC测试[5869:719056] BDRequestOptionAnimate

分析:

// 首先定义一组
typedef NS_OPTIONS(NSUInteger, BDRequestOptions) {
    BDRequestOptionSuccess     = 0b0001 << 0,
    BDRequestOptionFailure     = 0b0010 << 1,
    BDRequestOptionProcessing  = 0b0100 << 2,
    BDRequestOptionAnimate     = 0b1000 << 3,
};

#pragma mark - View lifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    
    [self test:BDRequestOptionSuccess | BDRequestOptionFailure | BDRequestOptionProcessing | BDRequestOptionAnimate];
    /** 
     BDRequestOptionSuccess | BDRequestOptionFailure | BDRequestOptionProcessing | BDRequestOptionAnimate
     
     等同于:0b0001 |
           0b0010 |
           0b0100 |
           0b1000 
     结果为:0b1111
     */
}

- (void)test:(BDRequestOptions)type {
    // 0b1111 & 0b0001 --->  0b0b0001
    if (type & BDRequestOptionSuccess) {
        NSLog(@"BDRequestOptionSuccess");
    }
    // 0b1111 & 0b0010 --->  0b0b0010
    if (type & BDRequestOptionFailure) {
        NSLog(@"BDRequestOptionFailure");
    }
    // 0b1111 & 0b0100 --->  0b0b0100
    if (type & BDRequestOptionProcessing) {
        NSLog(@"BDRequestOptionProcessing");
    }
    // 0b1111 & 0b1000 --->  0b0b1000
    if (type & BDRequestOptionAnimate) {
        NSLog(@"BDRequestOptionAnimate");
    }
}

 

另,默认的,如果开发中枚举值传0,意味着不做任何操作。

例如:

// 传0,不打印任何值
[self test:0];

 

 

OC中的用法:

NSString *string = @"Learning";
    [string boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.view.frame), MAXFLOAT)
                         options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine
                      attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12.f]}
                         context:nil];

上面传值:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine

逻辑处理:

 1     // 对传入的option逻辑处理
 2     if (option & NSStringDrawingUsesLineFragmentOrigin) {
 3         // 包含   NSStringDrawingUsesLineFragmentOrigin
 4     } else {
 5         // 未包含 NSStringDrawingUsesLineFragmentOrigin
 6     }
 7     if (option & NSStringDrawingTruncatesLastVisibleLine) {
 8         // 包含   NSStringDrawingTruncatesLastVisibleLine
 9     } else {
10         // 未包含 NSStringDrawingTruncatesLastVisibleLine
11     }

对于位移枚举的具体使用方法,建议可以查看一些三方库,例如 SDWebImage等!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值