枚举增强程序的可读性,用法上还是需要注意的
1.C语言的写法
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的时候会报错
这个地方需要这样改
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
}
}
在这里首先讲如何简单的使用, 仅仅是使用层面(有理解错误的地方帮忙纠正), 然后我们在去理解位运算符! 在下面的图中我们可以看见枚举值中有<<
(位运算符:左移):
如果我们在枚举值中看见<<
那我们就可以通过|
(位运算符:或)进行组合使用
如下代码为例:
//随便添加一个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;
的组合的值, 判断结果是:1
是nameA
的值, 2
是nameB
的值, nameC
与nameD
没有组合使用所以值为0
,最后我们知道如果value & nameC
为0
说明value
不包含nameC
相反则包含!
还有一点就是value & nameA
就是nameA
的值为1
, value & nameB
就是nameB
的值为2
-
<<
(左移):a << b
就表示把a
转为二进制后左移b
位(在后面添b
个0
) -
|
(或):只要有一个为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_ENUM
与NS_OPTIONS
)
NS_ENUM
和NS_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_OPTIONS
与 NS_ENUM
和 enum
是有什么区别呢?
1.通过上面介绍我们可以看出enum
可以声明一般类型和位掩码(bitmasked)类型
2.NS_ENUM
声明一般类型, NS_OPTIONS
声明掩码(bitmasked)类型
3.那么问题又来了, 直接用enum
不就可以了? 答案不是这样的, 苹果建议我们在OC中使用NS_ENUM
与NS_OPTIONS
, 为什么呢? 因为他们除了推断出不同类型的枚举,再就是当编译Objective-C++
模式,它们产生的代码是不同的, 就是因为不同所以混编的时候使用enum
会报错!