博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
枚举enum、NS_ENUM 、NS_OPTIONS
阅读量:6720 次
发布时间:2019-06-25

本文共 6950 字,大约阅读时间需要 23 分钟。

hot3.png

enum

 

了解位移枚举之前,我们先回顾一下C语言位运算符。

1     << : 左移,比如1<<n,表示1往左移n位,即数值大小2的n次方; 例如 : 0b0001 << 1 变为了 0b0010

2     >> : 右移,类似左移,数值大小除以2的n次方

3     &  : 按位与,1与任意数等于任意数本身,0与任意数等于0,即1&x=x,0&x=0
4     |  : 按位或,x|y中只要有一个1则结果为1;反之为0
5     ^  : 按位异或,x^y相等则为0,不等则为1

 

 

typedef enum A {

    a = 0,
    b,
    c,
    d,
} englishWord;
typedef enum {
    e = 4,
    f,
    g,
} englishWord2;
englishWord eg1 = a;
englishWord2 eg2 = e;
// enum newNum:NSInteger枚举变量,并且继承NSInteger;englishWord3 枚举的别名
typedef enum newNum:NSInteger englishWord3;
enum newNum:NSInteger {
    new1 = 10,
    new2,
};
englishWord3 eg3 = new1;

eg1 = 0,eg2 = 4, eg3 = 10

 

2、NS_ENUM 、NS_OPTIONS

OC中常见的枚举,例如常见的:

typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {

UIViewAnimationCurveEaseInOut,         // slow at beginning and end

UIViewAnimationCurveEaseIn,            // slow at beginning

UIViewAnimationCurveEaseOut,           // slow at end

UIViewAnimationCurveLinear

};

typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {

UIViewAnimationOptionLayoutSubviews            = 1 <<  0,

UIViewAnimationOptionAllowUserInteraction      = 1 <<  1, // turn on user interaction while animating

UIViewAnimationOptionBeginFromCurrentState     = 1 <<  2, // start all views from current value, not initial value

UIViewAnimationOptionRepeat                    = 1 <<  3, // repeat animation indefinitely

UIViewAnimationOptionAutoreverse               = 1 <<  4, // if repeat, run animation back and forth

UIViewAnimationOptionOverrideInheritedDuration = 1 <<  5, // ignore nested duration

UIViewAnimationOptionOverrideInheritedCurve    = 1 <<  6, // ignore nested curve

UIViewAnimationOptionAllowAnimatedContent      = 1 <<  7, // animate contents (applies to transitions only)

UIViewAnimationOptionShowHideTransitionViews   = 1 <<  8, // flip to/from hidden state instead of adding/removing

UIViewAnimationOptionOverrideInheritedOptions  = 1 <<  9, // do not inherit any options or animation type

UIViewAnimationOptionCurveEaseInOut            = 0 << 16, // default

UIViewAnimationOptionCurveEaseIn               = 1 << 16,

UIViewAnimationOptionCurveEaseOut              = 2 << 16,

UIViewAnimationOptionCurveLinear               = 3 << 16,

UIViewAnimationOptionTransitionNone            = 0 << 20, // default

UIViewAnimationOptionTransitionFlipFromLeft    = 1 << 20,

UIViewAnimationOptionTransitionFlipFromRight   = 2 << 20,

UIViewAnimationOptionTransitionCurlUp          = 3 << 20,

UIViewAnimationOptionTransitionCurlDown        = 4 << 20,

UIViewAnimationOptionTransitionCrossDissolve   = 5 << 20,

UIViewAnimationOptionTransitionFlipFromTop     = 6 << 20,

UIViewAnimationOptionTransitionFlipFromBottom  = 7 << 20,

} NS_ENUM_AVAILABLE_IOS(4_0);

 

这两个宏的定义在Foundation.framework的NSObjCRuntime.h中:

#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(NSInteger, UIViewAnimationTransition) {

展开得到:

  1. typedef enum UIViewAnimationTransition : NSInteger UIViewAnimationTransition;  
  2. enum UIViewAnimationTransition : NSInteger {

 

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

 

开发中,你也许见到过或用过类似这种的枚举类型:

typedef NS_OPTIONS(NSUInteger, BDRequestOptions) {

    BDRequestOptionSuccess     1 << 0,

    BDRequestOptionFailure     = 1 << 1,
    BDRequestOptionProcessing 
1 << 2,
    BDRequestOptionAnimate     
1 << 3,
};

其实这种的并不是枚举,而是按位掩码(bitmask),他的语法和枚举相同。但用法却不同。

示例:

// 首先定义一组typedef NS_OPTIONS(NSUInteger, BDRequestOptions) {    BDRequestOptionSuccess     = 1 << 0,    BDRequestOptionFailure     = 1 << 1,    BDRequestOptionProcessing  = 1 << 2,    BDRequestOptionAnimate     = 1 << 3,};// 然后调用我们定义的方法#pragma mark - View lifeCycle- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor orangeColor];        [self test:BDRequestOptionSuccess | BDRequestOptionFailure | BDRequestOptionProcessing | BDRequestOptionAnimate];}- (void)test:(BDRequestOptions)type {    if (type & BDRequestOptionSuccess) {        NSLog(@"BDRequestOptionSuccess");    }    if (type & BDRequestOptionFailure) {        NSLog(@"BDRequestOptionFailure");    }    if (type & BDRequestOptionProcessing) {        NSLog(@"BDRequestOptionProcessing");    }    if (type & BDRequestOptionAnimate) {        NSLog(@"BDRequestOptionAnimate");    }}// 查看打印结果:2016-04-04 14:09:44.946 OC测试[5869:719056] BDRequestOptionSuccess2016-04-04 14:09:44.947 OC测试[5869:719056] BDRequestOptionFailure2016-04-04 14:09:44.947 OC测试[5869:719056] BDRequestOptionProcessing2016-04-04 14:09:44.947 OC测试[5869:719056] BDRequestOptionAnimate

分析:

// 首先定义一组typedef NS_OPTIONS(NSUInteger, BDRequestOptions) {    BDRequestOptionSuccess     = 0b0001 << 0,    BDRequestOptionFailure     = 0b0010 << 1,    BDRequestOptionProcessing  = 0b0100 << 2,    BDRequestOptionAnimate     = 0b1000 << 3,};#pragma mark - View lifeCycle- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor orangeColor];        [self test:BDRequestOptionSuccess | BDRequestOptionFailure | BDRequestOptionProcessing | BDRequestOptionAnimate];    /**      BDRequestOptionSuccess | BDRequestOptionFailure | BDRequestOptionProcessing | BDRequestOptionAnimate          等同于:0b0001 |           0b0010 |           0b0100 |           0b1000      结果为:0b1111     */}- (void)test:(BDRequestOptions)type {    // 0b1111 & 0b0001 --->  0b0b0001    if (type & BDRequestOptionSuccess) {        NSLog(@"BDRequestOptionSuccess");    }    // 0b1111 & 0b0010 --->  0b0b0010    if (type & BDRequestOptionFailure) {        NSLog(@"BDRequestOptionFailure");    }    // 0b1111 & 0b0100 --->  0b0b0100    if (type & BDRequestOptionProcessing) {        NSLog(@"BDRequestOptionProcessing");    }    // 0b1111 & 0b1000 --->  0b0b1000    if (type & BDRequestOptionAnimate) {        NSLog(@"BDRequestOptionAnimate");    }}

 

另,默认的,如果开发中枚举值传0,意味着不做任何操作。

例如:

// 传0,不打印任何值[self test:0];

 

 

OC中的用法:

NSString *string = @"Learning";    [string boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.view.frame), MAXFLOAT)                         options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine                      attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12.f]}                         context:nil];

上面传值:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine

逻辑处理:

1     // 对传入的option逻辑处理 2     if (option & NSStringDrawingUsesLineFragmentOrigin) { 3         // 包含   NSStringDrawingUsesLineFragmentOrigin 4     } else { 5         // 未包含 NSStringDrawingUsesLineFragmentOrigin 6     } 7     if (option & NSStringDrawingTruncatesLastVisibleLine) { 8         // 包含   NSStringDrawingTruncatesLastVisibleLine 9     } else {10         // 未包含 NSStringDrawingTruncatesLastVisibleLine11     }

 

对于位移枚举的具体使用方法,建议可以查看一些三方库,例如等!

转载于:https://my.oschina.net/u/2320280/blog/729447

你可能感兴趣的文章
集合框架(集合的继承体系图解)
查看>>
Win32应用程序(SDK)设计原理详解
查看>>
windows serve 2012部署操作系统之部署前期准备(九)
查看>>
JFinal整合HTTL模板引擎
查看>>
“Object "netns" is unknown, try "ip help".\n'”报错
查看>>
SQL语句中----删除表数据drop、truncate和delete的用法
查看>>
零零散散学算法之详解几种数据存储结构
查看>>
我的友情链接
查看>>
关于vmware station 12pro 简易安装
查看>>
有用的正则表达式
查看>>
mysql show status解释
查看>>
Spark 下操作 HBase(1.0.0 新 API)
查看>>
PostgreSQL数据库切割和组合字段函数
查看>>
Jboss & Wildfly
查看>>
.NET简谈组件程序设计之(渗入序列化过程)
查看>>
DataGuard参数配置详解
查看>>
2010(Flex 初次使用 小节:No.2)
查看>>
VirtualBox 共享文件夹自动挂载
查看>>
PHP中使用PDO执行LIMIT语句无结果的问题
查看>>
apache日志统计工具
查看>>