NS_OPTIONS与NS_ENUM
开发中常用NS_ENUM
和NS_OPTIONS
来定义枚举,二者有何区别?使用时该如何抉择呢?
总结:只要枚举值需要用到按位或(2个及以上枚举值可多个存在)就使用NS_OPTIONS,否则使用NS_ENUM。
NS_OPTIONS
typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) { UISwipeGestureRecognizerDirectionNone = 0, //值为0 UISwipeGestureRecognizerDirectionRight = 1 << 0, //值为2的0次方 UISwipeGestureRecognizerDirectionLeft = 1 << 1, //值为2的1次方 UISwipeGestureRecognizerDirectionUp = 1 << 2, //值为2的2次方 UISwipeGestureRecognizerDirectionDown = 1 << 3 //值为2的3次方 };
小括号中第一个为NSUInteger这个为固定值,第二个为枚举类型,自己定义,大括号中枚举项必须全部包含小括号的枚举类型,枚举项后面再跟上几个值的区别,这里枚举项是NSUInteger类型,它的值我已经标记了,看上面注释,当然也可以像下方这样写枚举,但是官方推荐格式为上面那种。
typedef enum { UISwipeGestureRecognizerDirectionNone = 0, //值为0 UISwipeGestureRecognizerDirectionRight = 1 << 0, //值为2的0次方 UISwipeGestureRecognizerDirectionLeft = 1 << 1, //值为2的1次方 UISwipeGestureRecognizerDirectionUp = 1 << 2, //值为2的2次方 UISwipeGestureRecognizerDirectionDown = 1 << 3 //值为2的3次方 }UISwipeGestureRecognizerDirection;
NS_ENUM
typedef NS_ENUM(NSInteger, NSWritingDirection) { NSWritingDirectionNatural = -1, //值为-1 NSWritingDirectionLeftToRight = 0, //值为0 NSWritingDirectionRightToLeft = 1 //值为1 };
小括号中第一个为NSInteger这个为固定值,第二个为枚举类型,自己定义,大括号中枚举项必须包含小括号中自己定义的枚举类型,枚举项自己加后缀以视区别,大括号中的枚举项的值可自定义,若是定义了枚举项其中一项的值后面依次在它的前一项的值上加1,如这样:
typedef NS_ENUM(NSInteger, NSWritingDirection) { NSWritingDirectionNatural = 0, //值为0 NSWritingDirectionLeftToRight, //值为1 NSWritingDirectionRightToLeft //值为2 }; //或者这样 typedef NS_ENUM(NSInteger, NSWritingDirection) { NSWritingDirectionNatural = 0, //值为0 NSWritingDirectionLeftToRight = 2, //值为2 NSWritingDirectionRightToLeft //值为3 }; //若是都不定义值,默认第一项为0,后面依次枚举项的值加1。
当然也可以下方这样写枚举,但是官方不推荐,还是上面格式规范,
typedef enum { NSWritingDirectionNatural = -1, //值为-1 NSWritingDirectionLeftToRight = 0, //值为0 NSWritingDirectionRightToLeft = 1 //值为1 }NSWritingDirection;
UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc] init]; swipeGR.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
//这里几个枚举项同时存在表示它的方向同时包含1.向下2.向左3.向右
而NS_ENUM定义的枚举不能几个枚举项同时存在,只能选择其中一项,像这样:
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.baseWritingDirection = NSWritingDirectionNatural;
使用:
typedef NS_OPTIONS(NSInteger, TestType) { TestTypeOne = 1 << 1, // 0001 --> 0010 = 2 TestTypeTwo = 1 << 2, // 0001 --> 0100 = 4 TestTypeThree = 1 << 3, // 0001 --> 1000 = 8 };
TestType type = TestTypeOne | TestTypeTwo; //00000010 + 00000100 = 00000110 -> 6 if (type & TestTypeOne) { //00000110 & 00000010 = 00000010 -> 2 NSLog(@"包含 one"); } if (type & TestTypeTwo) { //00000110 & 00000100 = 00000100 -> 4 NSLog(@"包含 two"); } if (type & TestTypeThree) { //00000110 & 00001000 = 00000000 -> 0 NSLog(@"包含 three"); }
打印结果:
开发中常用NS_ENUM
和NS_OPTIONS
来定义枚举,二者有何区别?使用时该如何抉择呢?
总结:只要枚举值需要用到按位或(2个及以上枚举值可多个存在)就使用NS_OPTIONS,否则使用NS_ENUM。
NS_OPTIONS
typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
UISwipeGestureRecognizerDirectionNone = 0, //值为0
UISwipeGestureRecognizerDirectionRight = 1 << 0, //值为2的0次方
UISwipeGestureRecognizerDirectionLeft = 1 << 1, //值为2的1次方
UISwipeGestureRecognizerDirectionUp = 1 << 2, //值为2的2次方
UISwipeGestureRecognizerDirectionDown = 1 << 3 //值为2的3次方
};
小括号中第一个为NSUInteger这个为固定值,第二个为枚举类型,自己定义,大括号中枚举项必须全部包含小括号的枚举类型,枚举项后面再跟上几个值的区别,这里枚举项是NSUInteger类型,它的值我已经标记了,看上面注释,当然也可以像下方这样写枚举,但是官方推荐格式为上面那种。
typedef enum {
UISwipeGestureRecognizerDirectionNone = 0, //值为0
UISwipeGestureRecognizerDirectionRight = 1 << 0, //值为2的0次方
UISwipeGestureRecognizerDirectionLeft = 1 << 1, //值为2的1次方
UISwipeGestureRecognizerDirectionUp = 1 << 2, //值为2的2次方
UISwipeGestureRecognizerDirectionDown = 1 << 3 //值为2的3次方
}UISwipeGestureRecognizerDirection;
NS_ENUM
typedef NS_ENUM(NSInteger, NSWritingDirection) {
NSWritingDirectionNatural = -1, //值为-1
NSWritingDirectionLeftToRight = 0, //值为0
NSWritingDirectionRightToLeft = 1 //值为1
};
小括号中第一个为NSInteger这个为固定值,第二个为枚举类型,自己定义,大括号中枚举项必须包含小括号中自己定义的枚举类型,枚举项自己加后缀以视区别,大括号中的枚举项的值可自定义,若是定义了枚举项其中一项的值后面依次在它的前一项的值上加1,如这样:
typedef NS_ENUM(NSInteger, NSWritingDirection) {
NSWritingDirectionNatural = 0, //值为0
NSWritingDirectionLeftToRight, //值为1
NSWritingDirectionRightToLeft //值为2
};
//或者这样
typedef NS_ENUM(NSInteger, NSWritingDirection) {
NSWritingDirectionNatural = 0, //值为0
NSWritingDirectionLeftToRight = 2, //值为2
NSWritingDirectionRightToLeft //值为3
};
//若是都不定义值,默认第一项为0,后面依次枚举项的值加1。
当然也可以下方这样写枚举,但是官方不推荐,还是上面格式规范,
typedef enum {
NSWritingDirectionNatural = -1, //值为-1
NSWritingDirectionLeftToRight = 0, //值为0
NSWritingDirectionRightToLeft = 1 //值为1
}NSWritingDirection;
UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc] init]; swipeGR.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
//这里几个枚举项同时存在表示它的方向同时包含1.向下2.向左3.向右
而NS_ENUM定义的枚举不能几个枚举项同时存在,只能选择其中一项,像这样:
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.baseWritingDirection = NSWritingDirectionNatural;
使用:
typedef NS_OPTIONS(NSInteger, TestType) {
TestTypeOne = 1 << 1, // 0001 --> 0010 = 2
TestTypeTwo = 1 << 2, // 0001 --> 0100 = 4
TestTypeThree = 1 << 3, // 0001 --> 1000 = 8
};
TestType type = TestTypeOne | TestTypeTwo; //00000010 + 00000100 = 00000110 -> 6
if (type & TestTypeOne) { //00000110 & 00000010 = 00000010 -> 2
NSLog(@"包含 one");
}
if (type & TestTypeTwo) { //00000110 & 00000100 = 00000100 -> 4
NSLog(@"包含 two");
}
if (type & TestTypeThree) { //00000110 & 00001000 = 00000000 -> 0
NSLog(@"包含 three");
}
打印结果: