2019独角兽企业重金招聘Python工程师标准>>>
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) {
展开得到:
- typedef enum UIViewAnimationTransition : NSInteger UIViewAnimationTransition;
- 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 ---> 0b0b0001if (type & BDRequestOptionSuccess) {NSLog(@"BDRequestOptionSuccess");}// 0b1111 & 0b0010 ---> 0b0b0010if (type & BDRequestOptionFailure) {NSLog(@"BDRequestOptionFailure");}// 0b1111 & 0b0100 ---> 0b0b0100if (type & BDRequestOptionProcessing) {NSLog(@"BDRequestOptionProcessing");}// 0b1111 & 0b1000 ---> 0b0b1000if (type & BDRequestOptionAnimate) {NSLog(@"BDRequestOptionAnimate");} }
另,默认的,如果开发中枚举值传0,意味着不做任何操作。
例如:
// 传0,不打印任何值 [self test:0];
OC中的用法:
NSString *string = @"Learning";[string boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.view.frame), MAXFLOAT)options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLineattributes:@{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等!