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
其实这种的并不是枚举,而是按位掩码(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 }