Objective-C枚舉的幾種定義方式與使用

  假設咱們須要表示網絡鏈接狀態,能夠用下列枚舉表示:網絡

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個有效方法)

相關文章
相關標籤/搜索