iOS 枚舉用法分析

枚舉增強(qiáng)程序的可讀性,用法上還是需要注意的

1.C語(yǔ)言的寫法

enum XMPPReconnectFlags
{
 kShouldReconnect   = 1 << 0,  // If set, disconnection was accidental, and autoReconnect may be used
 kMultipleChanges   = 1 << 1,  // If set, there have been reachability changes during a connection attempt
 kManuallyStarted   = 1 << 2,  // If set, we were started manually via manualStart method
 kQueryingDelegates = 1 << 3,  // If set, we are awaiting response(s) from the delegate(s)
};

或者是

typedef enum XMPPReconnectFlags
{
 kShouldReconnect   = 1 << 0,  // If set, disconnection was accidental, and autoReconnect may be used
 kMultipleChanges   = 1 << 1,  // If set, there have been reachability changes during a connection attempt
 kManuallyStarted   = 1 << 2,  // If set, we were started manually via manualStart method
 kQueryingDelegates = 1 << 3,  // If set, we are awaiting response(s) from the delegate(s)
}XMPPReconnectFlags;//此處的XMPPReconnectFlags為別名

2.OC的寫法

typedef NS_ENUM(NSInteger, XMPPMessageBodyType) {
    XMPPMessageBodyTypeText = 1,       //文本
    XMPPMessageBodyTypeImage,          //圖片
    XMPPMessageBodyTypeSound,          //語(yǔ)音
    XMPPMessageBodyTypeMap,            //地理信息(文本)
    XMPPMessageBodyTypeNotification,   //通知
    XMPPMessageBodyTypeCustom,         //自定義
    XMPPMessageBodyTypeVideo,          //視頻
    XMPPMessageBodyTypeExpression,     //表情
};

第一個(gè)枚舉值只要NSInteger類型,后面的依次累加,中間再重新賦值,后面的會(huì)從賦值處重新累加

3.NS_ENUM和NS_OPTIONS

NS_ENUM和NS_OPTIONS本質(zhì)是一樣的,僅僅從字面上來(lái)區(qū)分其用途。NS_ENUM是通用情況,NS_OPTIONS一般用來(lái)定義具有位移操作或特點(diǎn)的情況

typedef NS_OPTIONS(NSInteger, ShareViewItemType) {
    ShareViewItemTypeWX = 1,
    ShareViewItemTypeWXFC = 1 << 0,
    ShareViewItemTypeQQ= 1 << 1,
    ShareViewItemTypeWB= 1 << 1,
    ShareViewItemTypeReport= 1 << 3,
    ShareViewItemTypeBarrage = 1 << 4,
    ShareViewItemTypeAll = 1 << 5,
};

如果多選枚舉,那么需要這樣判斷

+ (instancetype)shareViewWithDic:(NSDictionary*)dic shareViewItemType:(ShareViewItemType)shareViewItemType;
{
    if (shareViewItemType == (ShareViewItemTypeWX|ShareViewItemTypeWXFC)) {
        // code
    }
}

4.官方的全選枚舉

<code>UIControl</code>的枚舉

typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
    UIControlEventTouchDown                                         = 1 <<  0,      // on all touch downs
    UIControlEventTouchDownRepeat                                   = 1 <<  1,      // on multiple touchdowns (tap count > 1)
    UIControlEventTouchDragInside                                   = 1 <<  2,
    UIControlEventTouchDragOutside                                  = 1 <<  3,
    UIControlEventTouchDragEnter                                    = 1 <<  4,
    UIControlEventTouchDragExit                                     = 1 <<  5,
    UIControlEventTouchUpInside                                     = 1 <<  6,
    UIControlEventTouchUpOutside                                    = 1 <<  7,
    UIControlEventTouchCancel                                       = 1 <<  8,

    UIControlEventValueChanged                                      = 1 << 12,     // sliders, etc.
    UIControlEventPrimaryActionTriggered NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 13,     // semantic action: for buttons, etc.

    UIControlEventEditingDidBegin                                   = 1 << 16,     // UITextField
    UIControlEventEditingChanged                                    = 1 << 17,
    UIControlEventEditingDidEnd                                     = 1 << 18,
    UIControlEventEditingDidEndOnExit                               = 1 << 19,     // 'return key' ending editing

    UIControlEventAllTouchEvents                                    = 0x00000FFF,  // for touch events
    UIControlEventAllEditingEvents                                  = 0x000F0000,  // for UITextField
    UIControlEventApplicationReserved                               = 0x0F000000,  // range available for application use
    UIControlEventSystemReserved                                    = 0xF0000000,  // range reserved for internal framework use
    UIControlEventAllEvents                                         = 0xFFFFFFFF
};

最后一個(gè)<code> UIControlEventAllEvents </code>賦值為0xFFFFFFFF,意為全選,實(shí)際工程使用沒(méi)有問(wèn)題,很方便,但是在
Archive的時(shí)候會(huì)報(bào)錯(cuò)


屏幕快照 2016-06-29 下午12.28.10.png

這個(gè)地方需要這樣改

typedef NS_OPTIONS(NSUInteger, ShareViewItemType) {
    ShareViewItemTypeWX = 1,
    ShareViewItemTypeWXFC = 1 << 0,
    ShareViewItemTypeQQ= 1 << 1,
    ShareViewItemTypeWB= 1 << 1,
    ShareViewItemTypeReport= 1 << 3,
    ShareViewItemTypeBarrage = 1 << 4,
    ShareViewItemTypeAll = 0xFFFFFFFF,
};

你可以這樣判斷條件

+ (instancetype)shareViewWithDic:(NSDictionary*)dic shareViewItemType:(ShareViewItemType)shareViewItemType;
{
// 入?yún)hareViewItemType為ShareViewItemTypeAll
    if (shareViewItemType & ShareViewItemTypeBarrage) {
        // code
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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