http://www./Question/dc2f1712-927e-444f-9fdf-e4e51e4a103d.html
http://blog.csdn.net/annkie/article/details/9877643
=========================
一般情况下,我们采用C风格的enum关键字可以定义枚举类型。 - 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;
-
枚举值一般是4个字节的int值,在64位系统上是8个字节。 在iOS6和Mac OS 10.8以后Apple引入了两个宏来重新定义这两个枚举类型,实际上是将enum定义和typedef合二为一,并且采用不同的宏来从代码角度来区分。 NS_OPTIONS一般用来定义位移相关操作的枚举值,我们可以参考UIKit.Framework的头文件,可以看到大量的枚举定义。 - typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
- UIViewAnimationTransitionNone,
- 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本质是一样的,仅仅从字面上来区分其用途。NS_ENUM是通用情况,NS_OPTIONS一般用来定义具有位移操作或特点的情况(bitmask)。 实际使用时,可以直接定义: - typedef enum : NSInteger {....} UIViewAnimationTransition;
等效于上述定义。 参考文档: 1. http:///ns_enum-ns_options/ 2.http:///blog/2012/11/ns_enum-and-ns_options/
=========================
我经过预处理后具有铿锵在 Xcode5 中的代码。 typedef NS_ENUM(NSInteger, MyStyle) {
MyStyleDefault,
MyStyleCustom
};
typedef NS_OPTIONS(NSInteger, MyOption) {
MyOption1 = 1 << 0,
MyOption2 = 1 << 1,
};
而且这拿到。 typedef enum MyStyle : NSInteger MyStyle; enum MyStyle : NSInteger {
MyStyleDefault,
MyStyleCustom
};
typedef enum MyOption : NSInteger MyOption; enum MyOption : NSInteger {
MyOption1 = 1 << 0,
MyOption2 = 1 << 1,
};
我知道 NS_OPTIONS 是一个位掩码,但那里是任何技术上的差异吗?或者这只是命名约定吗? 编辑 根据 NS_OPTIONS 的定义,它可能是编译器的兼容性。(特别是对 c + + 编译器) // In CFAvailability.h
// Enums and Options
#if (__cplusplus && __cplusplus >= 201103L && (__has_extension(cxx_strong_enums) || __has_feature(objc_fixed_enum))) || (!__cplusplus && __has_feature(objc_fixed_enum))
#define CF_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
#if (__cplusplus)
#define CF_OPTIONS(_type, _name) _type _name; enum : _type
#else
#define CF_OPTIONS(_type, _name) enum _name : _type _name; enum _name : _type
#endif
#else
#define CF_ENUM(_type, _name) _type _name; enum
#define CF_OPTIONS(_type, _name) _type _name; enum
#endif
中铿锵 __cplusplus 值是 199711,我不能测试这虽然为确切地是。 解决方法 1:那里是 enum 和一个位掩码 (选项) 之间的基本区别。您向列表独家国家使用 enum。当几个属性可以应用在同一时间使用一个位掩码。 在两种情况下,您使用整数,但你看看它们以不同的方式。与 enum 你看看数值,带你看看各个位的位屏蔽。 typedef NS_ENUM(NSInteger, MyStyle) {
MyStyleDefault,
MyStyleCustom
};
将只代表两个国家。您可以简单地检查它的相等性测试。 switch (style){
case MyStyleDefault:
// int is 0
break;
case MyStyleCustom:
// int is 1
break;
}
虽然位掩码将代表更多的国家。您检查的各个位逻辑或按位运算符。 typedef NS_OPTIONS(NSInteger, MyOption) {
MyOption1 = 1 << 0, // bits: 0001
MyOption2 = 1 << 1, // bits: 0010
};
if (option & MyOption1){ // last bit is 1
// bits are 0001 or 0011
}
if (option & MyOption2){ // second to last bit is 1
// bits are 0010 or 0011
}
if (option & MyOption1 & MyOption2){ // last two bits are 1
// bits are 0011
}
tl ; 博士Enum 给数字的名称。一个位掩码给双边投资条约的名称。
=========================
iOS枚举类型的定义和使用 枚举的定义 ViewController895.h - #import <UIKit/UIKit.h>
- @interface ViewController895 : UIViewController
-
-
- typedef enum {
- ENUM_ViewController895_ActionTypeStart=0,
- ENUM_ViewController895_ActionTypeStop,
- ENUM_ViewController895_ActionTypePause
- } ENUM_ViewController895_ActionType;
-
-
-
-
- @property (nonatomic,assign) NSInteger InActionType;
-
- @end
上面我们就在ViewController895.h定义了一个枚举类型,枚举类型的值默认是连续的自然数,例如例子中的 - ENUM_ViewController895_ActionTypeStart=0,
那么其后的就依次为1,2,3....所以一般只需要设置枚举中第一个的值就可以。 枚举的使用 在需要使用的地方,引入枚举定义所在的.h文件,例如现在要在ViewController896.h中使用上面定义的枚举,那么: ViewController896.h - #import <UIKit/UIKit.h>
- #import "ViewController895.h"
-
- @interface ViewController896 : UIViewController
-
- @end
将枚举定义的.h import进来 然后就可以使用了。 ViewController896.m - #import "ViewController896.h"
-
- @interface ViewController896 ()
-
- @end
-
- @implementation ViewController896
-
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.edgesForExtendedLayout=UIRectEdgeNone;
- self.view.backgroundColor=[UIColor whiteColor];
-
- UIButton* btnGo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- [btnGo setTitle:@"GO" forState:UIControlStateNormal];
- [btnGo setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
- btnGo.frame = CGRectMake(20, 120, 100, 40);
- btnGo.backgroundColor=[UIColor whiteColor];
- btnGo.layer.borderWidth=0.5;
- btnGo.layer.borderColor=[UIColor blueColor].CGColor;
- [btnGo addTarget:self action:@selector(_doClickGo:) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:btnGo];
- }
-
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- }
-
- -(void) _doClickGo : (UIButton*) sender {
- ViewController895* nextpage = [[ViewController895 alloc]init];
- nextpage.InActionType=ENUM_ViewController895_ActionTypePause;
- [self.navigationController pushViewController:nextpage animated:YES];
- }
代码中的: - nextpage.InActionType=ENUM_ViewController895_ActionTypePause;
就直接使用了之前定义的枚举类型数据:ENUM_ViewController895_ActionTypePause ,其值为2.
=========================
一般情况下,我们采用C风格的enum关键字可以定义枚举类型。 - 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;
枚举值一般是4个字节的int值,在64位系统上是8个字节。 在iOS6和Mac OS 10.8以后Apple引入了两个宏来重新定义这两个枚举类型,实际上是将enum定义和typedef合二为一,并且采用不同的宏来从代码角度来区分。 NS_OPTIONS一般用来定义位移相关操作的枚举值,我们可以参考UIKit.Framework的头文件,可以看到大量的枚举定义。 - typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
- UIViewAnimationTransitionNone,
- 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本质是一样的,仅仅从字面上来区分其用途。NS_ENUM是通用情况,NS_OPTIONS一般用来定义具有位移操作或特点的情况(bitmask)。 实际使用时,可以直接定义: - typedef enum : NSInteger {....} UIViewAnimationTransition;
等效于上述定义。 参考文档: 1. http:///ns_enum-ns_options/ 2.http:///blog/2012/11/ns_enum-and-ns_options/ 原文:http://blog.csdn.net/annkie/article/details/9877643 枚举其实很重要,特别是在应用开发初期,服务器端数据格式需要更改得情况下,枚举和宏都能是程序简洁,并且改动小。 网上有个人写的言简意赅,适合初学 转自:http://blog.csdn.net/ysy441088327/article/details/8012677 题记: 蛋疼的枚举, 千万别小视了! 进入正题: 首先要知道的是,枚举值 它是一个整形(int) 并且,它不参加内存的占用和释放 枚举定义变量即可直接使用,不用初始化. 枚举的定义如下: typedef enum { //以下是枚举成员 TestA = 0, TestB, TestC, TestD }Test;//枚举名称 亦可以如下定义(推荐:结构比较清晰): typedef NS_ENUM(NSInteger, Test1) { //以下是枚举成员 Test1A = 0, Test1B = 1, Test1C = 2, Test1D = 3 }; 枚举的定义还支持位运算的方式定义,如下: 等于号后面必须等于1 typedef NS_ENUM(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); 判断枚举变量是否包含某个固定的枚举值,使用前需要确保枚举值以及各个组合的唯一性: NSLog(@"%d %d %d %d %d",TestA,TestB,TestC,TestD,TestE); Test tes = (TestA|TestB); NSLog(@"%d",tes); NSLog(@"%d",(tes & TestA)); if ((tes & TestA)) { NSLog(@"有"); } else { NSLog(@"没有"); } NSLog(@"%d",(tes & TestB)); if ((tes & TestA)) { NSLog(@"有"); } else { NSLog(@"没有"); } NSLog(@"%d",(tes & TestC)); if ((tes & TestC)) { NSLog(@"有"); } else { NSLog(@"没有"); } 如果 没有包含,将返回0, 0表示false NO 则进入else 也可以随时为枚举变量累加某个值,但是要自己控制不要添加已经加入过的枚举值, 枚举变量的值不会有变动,但这样将会误导阅读代码的人 有累加,自然有累减了,如果累减不存在的枚举值, 那么本次累减的枚举值,会自动累加上去. tes^= TestE;
=========================
|