枚举作用
枚举值 它是一个整形(int) 并且,它不参与内存的占用和释放,枚举定义变量即可直接使用,不用初始化.
在代码中使用枚举的目的只有一个,那就是增加代码的可读性.
枚举类型
OC里枚举的两种类型.
NS_ENUM和NS_OPTIONS
- NS_ENUM类型
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
};
- NS_OPTIONS类型
typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {
//基础属性的设置
UIViewAnimationOptionLayoutSubviews = 1 << 0,//设置子视图随父视图展示动画
UIViewAnimationOptionAllowUserInteraction = 1 << 1,//允许在动画执行时用户与其进行交互
UIViewAnimationOptionBeginFromCurrentState = 1 << 2,//允许在动画执行时执行新的动画
UIViewAnimationOptionRepeat = 1 << 3,//设置动画循环执行
UIViewAnimationOptionAutoreverse = 1 << 4,//设置动画反向执行,必须和重复执行一起使用
UIViewAnimationOptionOverrideInheritedDuration = 1 << 5,//强制动画使用内层动画的时间值
UIViewAnimationOptionOverrideInheritedCurve = 1 << 6,//强制动画使用内层动画曲线值
UIViewAnimationOptionAllowAnimatedContent = 1 << 7,//设置动画视图实时刷新
UIViewAnimationOptionShowHideTransitionViews = 1 << 8,//设置视图切换时隐藏,而不是移除
UIViewAnimationOptionOverrideInheritedOptions = 1 << 9,//
//设置动画播放的线性效果
UIViewAnimationOptionCurveEaseInOut = 0 << 16,//淡入淡出 首末减速
UIViewAnimationOptionCurveEaseIn = 1 << 16,//淡入 初始减速
UIViewAnimationOptionCurveEaseOut = 2 << 16,//淡出 末尾减速
UIViewAnimationOptionCurveLinear = 3 << 16,//线性 匀速执行
//设置UIView切换效果
UIViewAnimationOptionTransitionNone = 0 << 20,
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);
上边的枚举应该有三部分,所有没句对应的以为也不一样。
- 其他写法
typedef enum {
verificateBtnType00 = 00,
verificateBtnType01 = 01,
verificateBtnType02 = 02,
verificateBtnType03 = 03,
verificateBtnType04 = 04,
verificateBtnType05 = 05,
verificateBtnType06 = 06,
verificateBtnType07 = 07,
verificateBtnType08 = 08,
verificateBtnType09 = 09,
verificateBtnType10 = 10,
verificateBtnType11 = 11,
verificateBtnType12 = 12,
verificateBtnType13 = 13,
verificateBtnType14 = 14,
verificateBtnType15 = 15,
verificateBtnTypeB1 = B1
} verificateBtnType;
注意:其他写法中会有两个错误。
-
08,09两个数会报错(我一直很天真的以为枚举就是一直常数下去,求解释)。8进制下8是无效的(invalid digit ‘8’ in octal constant)。
-
枚举只能是int类型
-
NS_ENUM这种声明出来的东西大部分是单选. NS_OPTIONS声明出来的大部分是多选
typedef NS_ENUM(uint, Test)
{
TestA,
TestB,
TestC,
TestD,
TestE,
TestF,
TestG,
TestH,
TestI,
TestJ,
TestK
};
// test
NSLog(@"%d %d %d %d %d %d %d %d %d %d %d",
TestA,
TestB,
TestC,
TestD,
TestE,
TestF,
TestG,
TestH,
TestI,
TestJ,
TestK);
Test tes0 = (TestA|TestB);
// 多个枚举的另一种写法, 12 = 4+8
Test tes1 = 12;
NSLog(@"%d",tes0);
// 是否包含TestE
NSLog(@"%d",tes1 & 4);
// 是否包含TestC
NSLog(@"%d",tes1 & 2);
NSLog(@"%d",(tes0 & TestA));
NSLog(@"%d",(tes0 & TestB));
- 打印值
//如果没有包含,将返回0
枚举test[5295:1819063] 0 1 2 3 4 5 6 7 8 9 10
枚举test[5295:1819063] 1
枚举test[5295:1819063] 4
枚举test[5295:1819063] 0
枚举test[5295:1819063] 0
枚举test[5295:1819063] 1
- 按位与操作
00000111 十进制7
&00000001 十进制1
结果就是 00000001,为
true.表示含有这个选项.
网友评论