标签:
位移枚举
演练
1 定义枚举类型
/// 操作类型枚举
typedef enum {
ActionTypeTop = 1 << 0,
ActionTypeBottom = 1 << 1,
ActionTypeLeft = 1 << 2,
ActionTypeRight = 1 << 3
} ActionType;
根据操作类型参数,做出不同的响应
操作类型可以任意组合
- (void)action:(ActionType)type {
if (type == 0) {
NSLog(@"无操作");
return;
}
if (type & ActionTypeTop) {
NSLog(@"Top %tu", type & ActionTypeTop);
}
if (type & ActionTypeBottom) {
NSLog(@"Bottom %tu", type & ActionTypeBottom);
}
if (type & ActionTypeLeft) {
NSLog(@"Left %tu", type & ActionTypeLeft);
}
if (type & ActionTypeRight) {
NSLog(@"Right %tu", type & ActionTypeRight);
}
}
ActionType type = ActionTypeTop | ActionTypeRight;
[self action:type];
代码小结
iOS 特有语法
位移枚举,可以使用 按位或 设置数值
数字枚举,直接使用枚举设置数值
typedef NS_OPTIONS(NSUInteger, ActionType) {
ActionTypeTop = 1 << 0,
ActionTypeBottom = 1 << 1,
ActionTypeLeft = 1 << 2,
ActionTypeRight = 1 << 3
};
标签:
原文地址:http://www.cnblogs.com/fakeCoder/p/5093721.html