枚举增强程序的可读性,用法上还是需要注意的
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
}
}