假设咱们须要表示网络链接状态,能够用下列枚举表示:网络
enum CSConnectionState { CSConnectionStateDisconnected, CSConnectionStateConnecting, CSConnectionStateConnected, };
然而定义枚举变量的方式却太不简介,要依如些语法编写:框架
enum CSConnectionState state = CSConnectionStateDisconnected;
如果每次不用敲入 enum 而只需写 CSConnectionState 就行了。要想这样作,则需使用typedef关键字从新定义枚举类型:spa
enum CSConnectionState { CSConnectionStateDisconnected, CSConnectionStateConnecting, CSConnectionStateConnected, }; typedef enum CSConnectionState CSConnectionState;
如今能够用简写的 CSConnectionState 来代替完整的 enum CSConnectionState 了:code
CSConnectionState state = CSConnectionStateDisconnected;
C++11标准修订了枚举的某些特性。blog
例如能够指明用何种“底层数据类型”来保存枚举类型的变量,还能够不使用编译器所分配的序号,而是手工指定某个枚举成员所对应的值:开发
enum CSConnectionState: NSUInteger { CSConnectionStateDisconnected = 1, CSConnectionStateConnecting, CSConnectionStateConnected, }; typedef enum CSConnectionState CSConnectionState;
上述代码把 CSConnectionStateDisconnected 的值设为1,而不使用编译器所分配的0,接下来的几个枚举的值会在上一个的基础上递增1。编译器
前面所述的枚举使用时,建立的枚举变量只能使用一个枚举值,由于网络链接状态只会同时出现一种状况,该枚举的全部枚举值都是互斥的。it
假设咱们须要表示选项,这些选项能够同时被选中,那么咱们就得将枚举值定义好,各选项能够经过枚举值 “按位或操做符” 来组合。例如 iOS UI 框架中就有以下枚举类型,用来表示某个视图应该如何在水平或垂直方向上调整大小:io
enum UIViewAutoresizing: NSUInteger { UIViewAutoresizingNone = 0, UIViewAutoresizingFlexibleLeftMargin = 1 << 0, UIViewAutoresizingFlexibleWidth = 1 << 1, UIViewAutoresizingFlexibleRightMargin = 1 << 2, UIViewAutoresizingFlexibleTopMargin = 1 << 3, UIViewAutoresizingFlexibleHeight = 1 << 4, UIViewAutoresizingFlexibleBottomMargin = 1 << 5 }; typedef enum UIViewAutoresizing UIViewAutoresizing;
用 “按位或操做符” 可组合多个选项,用 “按位与操做符” 便可判断出是否启用某个选项:编译
UIViewAutoresizing resizing = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; if (resizing & UIViewAutoresizingFlexibleWidth) { // UIViewAutoresizingFlexibleWidth is set }
Foundation框架中定义了一些辅助宏,NS_ENUM(NSUInteger, <#MyEnum#>) 与 NS_OPTIONS(NSUInteger, <#MyEnum#>) 用法以下:
typedef NS_ENUM(NSUInteger, CSConnectionState) { CSConnectionStateDisconnected, CSConnectionStateConnecting, CSConnectionStateConnected, }; typedef NS_OPTIONS(NSUInteger, CSDirection) { CSDirectionUp = 1 << 0, CSDirectionDown = 1 << 1, CSDirectionLeft = 1 << 2, CSDirectionRight = 1 << 3, };
这些宏的定义以下:
#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) //按C++模式编译 #define NS_OPTIONS(_type, _name) _type _name; enum: _type #else //不按C++模式编译 #define NS_OPTIONS(_type, _name) enum _name: _type _name; enum _name: _type #endif #else //不支持新特性 #define NS_ENUM(_type, _name) _type _name; enum _name #define NS_OPTION(_type, _name) _type _name; enum _name #endif
因为须要分别处理不一样状况,因此上述代码用多种方式来定义这两个宏。第一个 #if 用于判断编译器是否支持新式枚举,若支持新特性,那么用 NS_ENUM 宏所定义的枚举展开后就是:
typedef enum State : NSUInteger State; enum State: NSUInteger { StateDisconnected, StateConnecting, StateConnected, };
根据是否要将代码按 C++ 模式编译,NS_OPTIONS 宏的定义方式也有所不一样。若是不按C++编译,其展开方式就和 NS_ENUM 相同,那么NS_OPTIONS 宏所定义的枚举展开后就是:
typedef enum CSDirection: NSUInteger CSDirection; enum CSDirection: NSUInteger { CSDirectionUp = 1 << 0, CSDirectionDown = 1 << 1, CSDirectionLeft = 1 << 2, CSDirectionRight = 1 << 3, };
而后考虑如下代码:
CSDirection CSDirection = CSDirectionUp | CSDirectionLeft;
若编译器按 C++ 模式编译(也可能按Objective-C++模式编译),则会给出下列错误信息:
error: cannot initialize a variable of type 'CSDirection' with an rvalue of type 'int'
若是想编译折行代码,就要将 “按位或操做” 的结果显示转换为CSDirection。因此,在 C++ 模式下应该用另外一种方式定义 NS_OPTIONS 宏,以便省去类型转换操做。
鉴于此,凡是须要以 “按位或操做” 来组合的枚举都应使用 NS_OPTIONS 来定义。
说完新特性,咱们再来看看若编译器不支持新特性时 NS_ENUM 与 NS_OPTIONS 宏的定义,若不支持新特性,NS_ENUM 与 NS_OPTIONS 宏的展开方式以下:
typedef NSUInteger CSConnectionState; enum CSConnectionState { CSConnectionStateDisconnected, CSConnectionStateConnecting, CSConnectionStateConnected, }; typedef NSUInteger CSDirection; enum CSDirection { CSDirectionUp = 1 << 0, CSDirectionDown = 1 << 1, CSDirectionLeft = 1 << 2, CSDirectionRight = 1 << 3, };
注意:处理枚举类型的switch语句中不要实现default分之。这样的话,加入新枚举以后,编译器就会提示开发者:switch语句并未处理全部枚举。
(参考及引用文献:《Effective Objective-C 2.0》编写高质量iOS与OS X代码的52个有效方法)