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

假設(shè)我們需要表示網(wǎng)絡連接狀態(tài),可以用下列枚舉表示:

enum BNConnectionState {
    BNConnectionStateDisconnected,
    BNConnectionStateConnecting,
    BNConnectionStateConnected,
};

然而定義枚舉變量的方式卻太不簡介,要依如些語法編寫:

enum BNConnectionState state = BNConnectionStateDisconnected;

若是每次不用敲入 enum 而只需寫 BNConnectionState 就好了。要想這樣做,則需使用typedef關(guān)鍵字重新定義枚舉類型:

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

現(xiàn)在可以用簡寫的 BNConnectionState 來代替完整的 enum BNConnectionState 了:

BNConnectionState state = BNConnectionStateDisconnected;

C++11標準修訂了枚舉的某些特性。

例如可以指明用何種“底層數(shù)據(jù)類型”來保存枚舉類型的變量,還可以不使用編譯器所分配的序號,而是手工指定某個枚舉成員所對應的值:

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

上述代碼把 BNConnectionStateDisconnected 的值設(shè)為1,而不使用編譯器所分配的0,接下來的幾個枚舉的值會在上一個的基礎(chǔ)上遞增1。

前面所述的枚舉使用時,創(chuàng)建的枚舉變量只能使用一個枚舉值,因為網(wǎng)絡連接狀態(tài)只會同時出現(xiàn)一種情況,該枚舉的所有枚舉值都是互斥的。

假設(shè)我們需要表示選項,這些選項可以同時被選中,那么我們就得將枚舉值定義好,各選項可以通過枚舉值 “按位或操作符” 來組合。例如 iOS UI 框架中就有如下枚舉類型,用來表示某個視圖應該如何在水平或垂直方向上調(diào)整大?。?/p>

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

根據(jù)是否要將代碼按 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'

如果想編譯折行代碼,就要將 “按位或操作” 的結(jié)果顯示轉(zhuǎn)換為BNDirection。所以,在 C++ 模式下應該用另一種方式定義 NS_OPTIONS 宏,以便省去類型轉(zhuǎn)換操作。

鑒于此,凡是需要以 “按位或操作” 來組合的枚舉都應使用 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語句中不要實現(xiàn)default分之。這樣的話,加入新枚舉之后,編譯器就會提示開發(fā)者:switch語句并未處理所有枚舉。

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容