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

本文介绍了在iOS开发中如何使用枚举enum,以及苹果在iOS6和Mac OS 10.8后引入的NS_ENUM和NS_OPTIONS宏。NS_OPTIONS通常用于定义具有位运算特性的枚举。此外,还探讨了位运算符在枚举中的应用,特别是在定义多个枚举值组合时的位移和位或操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

--参考文章:http://blog.youkuaiyun.com/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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值