iOS:枚举enum,NS_ENUM,NS_OPTIONS 定义和 位运算应用

来源:互联网 发布:欧陆风云4mac修改时间 编辑:程序博客网 时间:2024/05/16 15:52

--参考文章:http://blog.csdn.net/annkie/article/details/9877643

============枚举定义============

--1.C语言中定义枚举enum类型,参考文章《枚举类型enum 使用》 

--2.在iOS6和Mac OS 10.8以后Apple引入了两个宏NS_ENUMNS_OPTIONS来重新定义枚举类型,实际上是将enum定义和typedef合二为一。具体:

typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {    UIViewAnimationTransitionNone,//默认从0开始    UIViewAnimationTransitionFlipFromLeft,    UIViewAnimationTransitionFlipFromRight,    UIViewAnimationTransitionCurlUp,    UIViewAnimationTransitionCurlDown,};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};
它们的实际意义为:
#if (__cplusplus && __cplusplus >= 201103L && (__has_extension(cxx_strong_enums) || __has_feature(objc_fixed_enum))) || (!__cplusplus && __has_feature(objc_fixed_enum))#define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type#if (__cplusplus)#define NS_OPTIONS(_type, _name) _type _name; enum : _type#else#define NS_OPTIONS(_type, _name) enum _name : _type _name; enum _name : _type#endif#else#define NS_ENUM(_type, _name) _type _name; enum#define NS_OPTIONS(_type, _name) _type _name; enum#endif
即:
typedef NS_ENUM/NS_OPTIONS(NSInteger, UIViewAnimationTransition) {//相当于typedef enum UIViewAnimationTransition : NSInteger UIViewAnimationTransition;enum UIViewAnimationTransition : NSInteger {//即是typedef enum : NSInteger {....} UIViewAnimationTransition;

实际上,NS_ENUM和NS_OPTIONS本质是一样的,仅仅从字面上来区分其用途。NS_ENUM是通用情况,NS_OPTIONS一般用来定义具有位移操作或特点的情况

--注:关于枚举类型后面冒号,枚举中每个元素的基础类型是 int。 可以使用冒号指定另一种整数值类型(byte,long)

==================================

============位运算符应用============

--‘位移操作’定义:当枚举变量的某些取值不是只表示某个枚举成员的意义,而是要表示多个枚举成员的意思。那么枚举成员之间关系,而是一种‘可结合不冲突’的关系,枚举成员的值可以用‘位移操作’定义

typedef NS_OPTIONS(NSUInteger, NSCalendarUnit) {        NSCalendarUnitEra                = kCFCalendarUnitEra,        NSCalendarUnitYear               = kCFCalendarUnitYear,        NSCalendarUnitMonth              = kCFCalendarUnitMonth,        NSCalendarUnitDay                = kCFCalendarUnitDay,        NSCalendarUnitHour               = kCFCalendarUnitHour,        NSCalendarUnitMinute             = kCFCalendarUnitMinute,        NSCalendarUnitSecond             = kCFCalendarUnitSecond,        NSCalendarUnitWeekday            = kCFCalendarUnitWeekday,        NSCalendarUnitWeekdayOrdinal     = kCFCalendarUnitWeekdayOrdinal,        ……    }
 NSLog(@"(%d,%d,%d,%d,%d,%d)",NSCalendarUnitEra,NSCalendarUnitYear,NSCalendarUnitMonth,NSCalendarUnitDay,NSCalendarUnitHour,NSCalendarUnitMinute);//输出:(2,4,8,16,32,64)
--单个枚举值:当使用这个枚举的时候,如果想得到某个枚举值对应的对象,参数可以为某个枚举值--NSYearCalendarUnit
 NSCalendar *calendar = [NSCalendar currentCalendar];    NSDate *date = [NSDate date];    NSDateComponents *components ;    NSLog(@"initialDate:%@",date);    //initialDate:2014-05-15 03:18:35 +0000        components = [calendar components:NSYearCalendarUnit fromDate:date];    NSLog(@"components1:%@",components);    NSLog(@"date1:%@",[calendar dateFromComponents:components]);    //components1:<NSDateComponents: 0x8a5b900>Calendar Year: 2014    //date1:2013-12-31 16:00:00

--多个枚举值位或:如果想得到几个枚举值对应的对象,参数可以为几个枚举值‘位或’值

例如:--NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit...

 components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];    NSLog(@"components2:%@",components);    NSLog(@"date2:%@",[calendar dateFromComponents:components]);    //components2:<NSDateComponents: 0x8a5abd0> Calendar Year: 2014  Month: 5  Leap month: no Day: 15    //date2:2014-05-14 16:00:00
----备注:当components“不完整”的时候,用它去取得date,这个值是不准确的。见上文components1只有NSYearCalendarUnit
例如:字符串比较方式NSCaseInsensitiveSearch|NSLiteralSearch|NSNumericSearch...
    int result1 = [@"Foo3" compare:@"foo25" options:NSCaseInsensitiveSearch];    NSLog(@"The result is %d",result1); // 1    int result2 = [@"foo3" compare:@"foo25" options:NSNumericSearch];    NSLog(@"The result is %d",result2); // -1    int result3 = [@"Foo3" compare:@"foo25" options:NSCaseInsensitiveSearch|NSNumericSearch];    NSLog(@"The result is %d",result3); // -1

0 0
原创粉丝点击