背景
一直对OC中的位移操作枚举不太理解,查找到两篇介绍OC中枚举的文章,觉得很不错。
什么是位移操作枚举呢?
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
};
参考文章
枚举介绍
枚举值一般是4个字节的int值,在64位系统上是8个字节。它不参与内存的占用和释放,枚举定义变量即可直接使用,不用初始化。
在代码中使用枚举的目的只有一个,那就是增加代码的可读性。
C语言风格的枚举定义
enum{
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
} UIViewAnimationTransition;
//位移操作枚举定义
enum {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
typedef NSUInteger UIViewAutoresizing;
//使用NSUInteger的地方可以使用UIViewAutoresizing,
//UIViewAutoresizing相当于NSUInteger的一个别名使用。
//因此一个UIViewAutoresizing的变量可以直接赋值给NSUInteger
OC风格的枚举定义
在iOS6和Mac OS 10.8以后Apple引入了两个宏来重新定义这两个枚举类型,实际上是将enum定义和typedef合二为一,并且采用不同的宏来从代码角度来区分。NS_ENUM是通用情况,NS_OPTIONS一般用来定义具有位移操作或特点的情况。
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
UIViewAnimationTransitionNone,//默认从0开始
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
};
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
};
这两个宏的定义在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本质是一样的。实际使用时,也可以这样定义:
typedef enum : NSInteger {....} UIViewAnimationTransition;
什么场景下使用位移操作枚举呢?
那就是一个枚举变量可能要代表多个枚举值的时候。例如:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft;
}
上面UIInterfaceOrientationMask 一定是个位移操作枚举。
其实给一个枚举变量赋予多个枚举值的时候,原理只是把各个枚举值加起来罢了.
当加起来以后,就获取了一个新的值,那么为了保证这个值的唯一性,这个时候就体现了位运算的重要作用.
位运算可以确保枚举值组合的唯一性.
因为位运算的计算方式是将二进制转换成十进制,也就是说,枚举值里面存取的是 计算后的十进制值。
位运算可以确保枚举值组合的唯一性.
因为位运算的计算方式是将二进制转换成十进制,也就是说,枚举值里面存取的是 计算后的十进制值。
typedef NS_OPTIONS(NSInteger, Test)
{
TestA = 1, //1 1 1
TestB = 1 << 1, //2 2 10 转换成 10进制 2
TestC = 1 << 2, //4 3 100 转换成 10进制 4
TestD = 1 << 3, //8 4 1000 转换成 10进制 8
TestE = 1 << 4 //16 5 10000 转换成 10进制 16
};
通过上面的位运算方式设定好枚举以后,打印出来的枚举值分别是: 1 2 4 8 16
这5个数字,无论你如何组合在一起,也不会产生两个同样的数字.
多枚举值 赋值方式如下:
Test tes = (TestA|TestB);
判断枚举变量是否包含某个固定的枚举值,使用前需要确保枚举值以及各个组合的唯一性:
Test tes = (TestA|TestB);
NSLog(@"%ld",(long)tes);
NSLog(@"%u",(tes & TestA));
if ((tes & TestA)) {
NSLog(@"包含A");
}else
{
NSLog(@"不包含A");
}
NSLog(@"%u",(tes & TestC));
if ((tes & TestC)) {
NSLog(@"包含C");
}else
{
NSLog(@"不包含C");
}
也可以随时为枚举变量累加某个值
tes |=TestC;
注意:但是要自己控制不要添加已经加入过的枚举值, 枚举变量的值不会有变动,但这样将会误导阅读代码的人
有累加,自然有累减了
tes^= TestE;
注意:如果累减不存在的枚举值, 那么本次累减的枚举值,会自动累加上去.