枚举的作用:增强代码的可读性,
1.C语言的写法
enum LJCPPReconnectFlags
{
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)
};
2.OC中有两种写法
<p>1、typedef NS_ENUM(NSInteger, ****Type) 模式, 第一个枚举值只要NSInteger类型,后面的依次累加,中间再重新赋值,后面的会从赋值处重新累加</p>
typedef NS_ENUM(NSInteger, LJCMessageBodyType) {
LJCMessageBodyTypeText = 1, //文本
LJCMessageBodyTypeImage, //图片
LJCMessageBodyTypeSound, //语音
LJCMessageBodyTypeMap, //地理信息(文本)
LJCMessageBodyTypeNotification, //通知
LJCMessageBodyTypeCustom, //自定义
LJCMessageBodyTypeVideo, //视频
LJCMessageBodyTypeExpression, //表情
};
<p>2、typedef NS_OPTIONS(NSInteger, *****Type) 模式, 具有位移操作,</p>
typedef NS_OPTIONS(NSUInteger, LJCType) {
LJCTypeWX = 1,
LJCTypeWXFC = 1 << 0,
LJCTypeQQ= 1 << 1,
LJCTypeWB= 1 << 1,
LJCTypeReport= 1 << 3,
LJCTypeBarrage = 1 << 4,
LJCTypeAll = 0xFFFFFFFF,
};
<p> <<(左移):a << b就表示把a转为二进制后左移b位(在后面添b个0)</p>
<p> LJCTypeAll = 0xFFFFFFFF 表示全选 </p>
<p> 如果我们在枚举值中看见<<那我们就可以通过|(位运算符:或)进行组合使用如下代码为例: </p>
//随便添加一个UITextField
UITextField *field = [UITextField new];
//Begin,Changed,DidEnd都能触发UITextField的事件
[field addTarget:self action:@selector(textFieldDidChanged) forControlEvents: UIControlEventEditingDidBegin |
UIControlEventValueChanged |
UIControlEventEditingDidEnd
];
[self.view addSubview:field];