Objective-C枚举的几种定义办法与使用
发布时间:2021-11-18 16:06:38 所属栏目:教程 来源:互联网
导读:假设我们需要表示网络连接状态,可以用下列枚举表示: enum CSConnectionState { CSConnectionStateDisconnected, CSConnectionStateConnecting, CSConnectionStateConnected, }; 然而定义枚举变量的方式却太不简介,要依如些语法编写: enum CSConnectionSt
假设我们需要表示网络连接状态,可以用下列枚举表示: enum CSConnectionState { CSConnectionStateDisconnected, CSConnectionStateConnecting, CSConnectionStateConnected, }; 然而定义枚举变量的方式却太不简介,要依如些语法编写: enum CSConnectionState state = CSConnectionStateDisconnected; 若是每次不用敲入 enum 而只需写 CSConnectionState 就好了。要想这样做,则需使用typedef关键字重新定义枚举类型: enum CSConnectionState { CSConnectionStateDisconnected, CSConnectionStateConnecting, CSConnectionStateConnected, }; typedef enum CSConnectionState CSConnectionState; 现在可以用简写的 CSConnectionState 来代替完整的 enum CSConnectionState 了: CSConnectionState state = CSConnectionStateDisconnected; C++11标准修订了枚举的某些特性。 例如可以指明用何种“底层数据类型”来保存枚举类型的变量,还可以不使用编译器所分配的序号,而是手工指定某个枚举成员所对应的值: enum CSConnectionState: NSUInteger { CSConnectionStateDisconnected = 1, CSConnectionStateConnecting, CSConnectionStateConnected, }; typedef enum CSConnectionState CSConnectionState; 上述代码把 CSConnectionStateDisconnected 的值设为1,而不使用编译器所分配的0,接下来的几个枚举的值会在上一个的基础上递增1。 前面所述的枚举使用时,创建的枚举变量只能使用一个枚举值,因为网络连接状态只会同时出现一种情况,该枚举的所有枚举值都是互斥的。 假设我们需要表示选项,这些选项可以同时被选中,那么我们就得将枚举值定义好,各选项可以通过枚举值 “按位或操作符” 来组合。例如 iOS UI 框架中就有如下枚举类型,用来表示某个视图应该如何在水平或垂直方向上调整大小: 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, }; (编辑:东莞站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |