美文网首页
Objective-C枚举的几种定义方式与使用

Objective-C枚举的几种定义方式与使用

作者: Berning | 来源:发表于2018-09-05 11:39 被阅读3次

    假设我们需要表示网络连接状态,可以用下列枚举表示:

    enum BNConnectionState {
        BNConnectionStateDisconnected,
        BNConnectionStateConnecting,
        BNConnectionStateConnected,
    };
    

    然而定义枚举变量的方式却太不简介,要依如些语法编写:

    enum BNConnectionState state = BNConnectionStateDisconnected;
    

    若是每次不用敲入 enum 而只需写 BNConnectionState 就好了。要想这样做,则需使用typedef关键字重新定义枚举类型:

     enum BNConnectionState {
        BNConnectionStateDisconnected,
        BNConnectionStateConnecting,
        BNConnectionStateConnected,
    };
    typedef enum BNConnectionState BNConnectionState;
    

    现在可以用简写的 BNConnectionState 来代替完整的 enum BNConnectionState 了:

    BNConnectionState state = BNConnectionStateDisconnected;
    

    C++11标准修订了枚举的某些特性。

    例如可以指明用何种“底层数据类型”来保存枚举类型的变量,还可以不使用编译器所分配的序号,而是手工指定某个枚举成员所对应的值:

     enum BNConnectionState: NSUInteger {
        BNConnectionStateDisconnected = 1,
        BNConnectionStateConnecting,
        BNConnectionStateConnected,
    };
    typedef enum BNConnectionState BNConnectionState;
    

    上述代码把 BNConnectionStateDisconnected 的值设为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, BNConnectionState) {
        BNConnectionStateDisconnected,
        BNConnectionStateConnecting,
        BNConnectionStateConnected,
    };
    
     typedef NS_OPTIONS(NSUInteger, BNDirection) {
        BNDirectionUp    = 1 << 0,
        BNDirectionDown  = 1 << 1,
        BNDirectionLeft  = 1 << 2,
        BNDirectionRight = 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 BNDirection: NSUInteger BNDirection;
    enum BNDirection: NSUInteger {
        BNDirectionUp    = 1 << 0,
        BNDirectionDown  = 1 << 1,
        BNDirectionLeft  = 1 << 2,
        BNDirectionRight = 1 << 3,
    };
    

    然后考虑以下代码:

    BNDirection CSDirection = BNDirectionUp | BNDirectionLeft;
    

    若编译器按 C++ 模式编译(也可能按Objective-C++模式编译),则会给出下列错误信息:

    error: cannot initialize a variable of type
    'BNDirection' with an rvalue of type 'int'
    

    如果想编译折行代码,就要将 “按位或操作” 的结果显示转换为BNDirection。所以,在 C++ 模式下应该用另一种方式定义 NS_OPTIONS 宏,以便省去类型转换操作。

    鉴于此,凡是需要以 “按位或操作” 来组合的枚举都应使用 NS_OPTIONS 来定义。

    说完新特性,我们再来看看若编译器不支持新特性时 NS_ENUM 与 NS_OPTIONS 宏的定义,若不支持新特性,NS_ENUM 与 NS_OPTIONS 宏的展开方式如下:

    typedef NSUInteger BNConnectionState;
    enum BNConnectionState {
        BNConnectionStateDisconnected,
        BNConnectionStateConnecting,
        BNConnectionStateConnected,
    };
    
    typedef NSUInteger BNDirection;
    enum BNDirection {
        BNDirectionUp    = 1 << 0,
        BNDirectionDown  = 1 << 1,
        BNDirectionLeft  = 1 << 2,
        BNDirectionRight = 1 << 3,
    }; 
    

    注意:处理枚举类型的switch语句中不要实现default分之。这样的话,加入新枚举之后,编译器就会提示开发者:switch语句并未处理所有枚举。

    (参考及引用文献:《Effective Objective-C 2.0》编写高质量iOS与OS X代码的52个有效方法)

    相关文章

      网友评论

          本文标题:Objective-C枚举的几种定义方式与使用

          本文链接:https://www.haomeiwen.com/subject/hqxawftx.html