iOS 枚举讲解_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > iOS 枚举讲解

iOS 枚举讲解

 2017/10/19 19:25:54  路人Q  程序员俱乐部  我要评论(0)
  • 摘要:枚举增强程序的可读性,用法上还是需要注意的1.C语言的写法enumXMPPReconnectFlags{kShouldReconnect=1<<0,//Ifset,disconnectionwasaccidental,andautoReconnectmaybeusedkMultipleChanges=1<<1,//Ifset
  • 标签:iOS 枚举

枚举增强程序的可读性,用法上还是需要注意的

1.C语言的写法

class="hljs undefined">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,          //语音
    XMPPMessageBodyTypeMap,            //地理信息(文本)
    XMPPMessageBodyTypeNotification,   //通知
    XMPPMessageBodyTypeCustom,         //自定义
    XMPPMessageBodyTypeVideo,          //视频
    XMPPMessageBodyTypeExpression,     //表情
};

第一个枚举值只要NSInteger类型,后面的依次累加,中间再重新赋值,后面的会从赋值处重新累加

3.NS_ENUM和NS_OPTIONS

NS_ENUM和NS_OPTIONS本质是一样的,仅仅从字面上来区分其用途。NS_ENUM是通用情况,NS_OPTIONS一般用来定义具有位移操作或特点的情况

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.官方的全选枚举

UIControl的枚举

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

最后一个 UIControlEventAllEvents赋值为0xFFFFFFFF,意为全选,实际工程使用没有问题,很方便,但是在
Archive的时候会报错

imageView2/2/w/1240" alt="" data-original-src="http://upload-images.jianshu.io/upload_images/1282588-595925abb812cd50.png?imageMogr2/auto-orient/strip%7CimageView2/2">
屏幕快照 2016-06-29 下午12.28.10.png


这个地方需要这样改

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;
{
// 入参shareViewItemType为ShareViewItemTypeAll
    if (shareViewItemType & ShareViewItemTypeBarrage) {
        // code
    }
}

5.使用层面的理解

在这里首先讲如何简单的使用, 仅仅是使用层面(有理解错误的地方帮忙纠正), 然后我们在去理解位运算符! 在下面的图中我们可以看见枚举值中有<<(位运算符:左移):


 

如果我们在枚举值中看见<<那我们就可以通过|(位运算符:或)进行组合使用如下代码为例:

 //随便添加一个UITextField
 UITextField *field = [UITextField new];
 //Begin,Changed,DidEnd都能触发UITextField的事件
 [field addTarget:self action:@selector(textFieldDidChanged) forControlEvents: UIControlEventEditingDidBegin |
                  UIControlEventValueChanged |
                  UIControlEventEditingDidEnd
     ];

 [self.view addSubview:field];

如下图枚举值中没有<<,这就是普通的NSInteger类型的枚举, 所以不能组合使用:


 

那苹果官方是怎么知道我们多个条件组合使用了呢? 答案是通过&(位运算符:与)进行判断的:

//controlEvents是组合使用后的一个值
 NSUInteger controlEvents = UIControlEventEditingDidBegin | UIControlEventValueChanged | UIControlEventEditingDidEnd;
    /**
    //通过 & 来判断是否包含:
    UIControlEventEditingDidBegin,
    UIControlEventValueChanged,
    UIControlEventEditingDidEnd
     */
    if (controlEvents & UIControlEventEditingDidBegin) {

        NSLog(@"UIControlEventEditingDidBegin");

    }else if (controlEvents & UIControlEventValueChanged) {

        NSLog(@"UIControlEventValueChanged");

    }else if (controlEvents & UIControlEventEditingDidEnd) {

        NSLog(@"UIControlEventEditingDidEnd");
    }

那么我们接下来看看使用过程中牵扯到的位运算符, 我们会在下面举个例子!

6.理解位运算符

首先我们有一个枚举, 下面代码2种写法我们暂时先不用管,等位运算符讲完我们会讨论枚举的宏使用:

//typedef NS_OPTIONS(NSInteger, myTests) {
//    nameA = 1 << 0,
//    nameB = 1 << 1,
//    nameC = 1 << 2,
//    nameD = 1 << 3,
//};

typedef enum {
    nameA = 1 << 0,
    nameB = 1 << 1,
    nameC = 1 << 2,
    nameD = 1 << 3,

}myTests;

/**
 nameA = 1 << 0 :值为1(2的0次方)
 nameB = 1 << 1 :值为2(2的1次方)
 nameC = 1 << 2 :值为4(2的2次方)
 nameD = 1 << 3 :值为8(2的3次方)
 */

通过&进行判断我们来看看输出结果如下图:


 

我们得到NSInteger value = nameA | nameB;的组合的值, 判断结果是:1nameA的值, 2nameB的值, nameCnameD没有组合使用所以值为0,最后我们知道如果value & nameC0说明value不包含nameC 相反则包含!

还有一点就是value & nameA就是nameA的值为1, value & nameB就是nameB的值为2

  • <<(左移):a << b就表示把a转为二进制后左移b位(在后面添b0
  • |(或):只要有一个为1, 结果就是1
  • &(与):只要有二个为1, 结果才是1

我们已经知道nameA = 1, nameB = 2, nameC = 4, nameD = 8下面来通过二进制来解释:

 NSInteger value = nameA | nameB | nameC | nameD;
     转成二进制:
     nameA: 0 0 0 1
       |
     nameB: 0 0 1 0
       |
     nameC: 0 1 0 0
       |
     nameD: 1 0 0 0
    ----------------
     value: 1 1 1 1
     上面是使用 | 得出value的值为1111(|的意思是有一个为1结果就为1)


     下面是使用 & 判断输出的值(&的意思就是有二个为1结果才为1)

      value: 1 1 1 1         value: 1 1 1 1
        &                      &
      nameA: 0 0 0 1         nameB: 0 0 1 0
     ----------------       ----------------
      结果值: 0 0 0 1         结果值: 0 0 1 0

      我就写2个例子:0001就是nameA的值, 0010就是nameB的值

相信大家已经明白其中的道理了, 接下来我们来看看枚举的宏, 为了更好阅读也可以看下面的截图:


 

7.枚举的宏(NS_ENUMNS_OPTIONS)

NS_ENUMNS_OPTIONS宏提供了一个简洁、定义枚举和C语言选项的简单方法。

The NS_ENUM and NS_OPTIONS macros provide a concise, simple way of defining enumerations and options in C-based languages. These macros improve code completion in Xcode and explicitly specify the type and size of your enumerations and options. Additionally, this syntax declares enums in a way that is evaluated correctly by older compilers, and by newer ones that can interpret the underlying type information.

这是最初的使用方法:

enum {
        UITableViewCellStyleDefault,
        UITableViewCellStyleValue1,
        UITableViewCellStyleValue2,
        UITableViewCellStyleSubtitle
};
typedef NSInteger UITableViewCellStyle;

--------------------------------------------------

enum {
        UIViewAutoresizingNone                 = 0,
        UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
        UIViewAutoresizingFlexibleWidth        = 1 << 1,
        UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
        UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
        UIViewAutoresizingFlexibleHeight       = 1 << 4,
        UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
typedef NSUInteger UIViewAutoresizing;

通过使用枚举的宏:

NS_ENUM:是用来声明一般的NSInteger(下面代码使用NSInteger)类型的枚举

Use the NS_ENUM macro to define enumerations, a set of values that are mutually exclusive.

NS_OPTIONS:是用来声明位掩码(bitmasked)

Use the NS_OPTIONS macro to define options, a set of bitmasked values that may be combined together.

//NS_ENUM
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
        UITableViewCellStyleDefault,
        UITableViewCellStyleValue1,
        UITableViewCellStyleValue2,
        UITableViewCellStyleSubtitle
};

--------------------------------------------------

//NS_OPTIONS
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
        UIViewAutoresizingNone                 = 0,
        UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
        UIViewAutoresizingFlexibleWidth        = 1 << 1,
        UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
        UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
        UIViewAutoresizingFlexibleHeight       = 1 << 4,
        UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};

NS_OPTIONSNS_ENUMenum 是有什么区别呢?

1.通过上面介绍我们可以看出enum可以声明一般类型和位掩码(bitmasked)类型

2.NS_ENUM声明一般类型, NS_OPTIONS声明掩码(bitmasked)类型

3.那么问题又来了, 直接用enum不就可以了? 答案不是这样的, 苹果建议我们在OC中使用NS_ENUMNS_OPTIONS, 为什么呢? 因为他们除了推断出不同类型的枚举,再就是当编译Objective-C++模式,它们产生的代码是不同的, 就是因为不同所以混编的时候使用enum会报错!

 

上一篇: gradle boot log4j2 下一篇: 没有下一篇了!
发表评论
用户名: 匿名