歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Objective-C枚舉的幾種定義方式與使用

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

日期:2017/3/1 9:11:40   编辑:Linux編程

  假設我們需要表示網絡連接狀態,可以用下列枚舉表示:

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,
};   

  注意:處理枚舉類型的switch語句中不要實現default分之。這樣的話,加入新枚舉之後,編譯器就會提示開發者:switch語句並未處理所有枚舉。

  (參考及引用文獻:《Effective Objective-C 2.0》編寫高質量iOS與OS X代碼的52個有效方法)

Objective-C中@property的所有屬性詳解 http://www.linuxidc.com/Linux/2014-03/97744.htm

Objective-C 和 Core Foundation 對象相互轉換的內存管理總結 http://www.linuxidc.com/Linux/2014-03/97626.htm

使用 Objective-C 一年後我對它的看法 http://www.linuxidc.com/Linux/2013-12/94309.htm

10個Objective-C基礎面試題,iOS面試必備 http://www.linuxidc.com/Linux/2013-07/87393.htm

Objective-C適用C數學函數 <math.h> http://www.linuxidc.com/Linux/2013-06/86215.htm

好學的 Objective-C 高清PDF http://www.linuxidc.com/Linux/2014-09/106226.htm

Copyright © Linux教程網 All Rights Reserved