枚举及使用

111 阅读2分钟

枚举:在C/C++/c#,还有Objective-C中,是一个 被命名的整型常数集合。(用来定义 多个(有意义的)常量的)

而枚举在iOS开发过程中,一系列常量来表示状态或者可选项时使用! 枚举值一般是4字节int值,在64位系统上是8字节。枚举仅仅是一种常量命名方式,为了增强代码的可读性


普通模式: enum

C语言中定义枚举,使用enum类型

例子:即时通讯的类型

typedef enum : int {
    //个人
    PeopleConversationType = 0,  // “0”可要可不要(默认从0开始)

    //群聊
    GroupConversationType
} ConversationType;   

或者:

typedef enum {  // 默认:4字节int
    //个人
    PeopleConversationType  = 0,
    
    //群聊
    GroupConversationType,    // “,”可要可不要
    
} ConversationType;

参考:数组/枚举 初始化最后一个逗号的问题



在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_OPTIONSNSInteger, UIViewAnimationTransition) {  

//相当于  
typedef enum UIViewAnimationTransition : NSInteger UIViewAnimationTransition;  
enum UIViewAnimationTransition : NSInteger {  

//即是  
typedef enum : NSInteger {....} UIViewAnimationTransition; 



通用情况 使用枚举“NS_ENUM:

唯一区别是:可判断 编译器是否支持新式枚举,支持使用 的,否则使用旧的。

例子:

typedef NS_ENUM(int, ConversationType) {
    //个人
    PeopleConversationType = 0,

    //群聊
    GroupConversationType
};

使用:

//定义
ConversationType convType = PeopleConversationType;

//使用------判断
switch (convType)
{
   case PeopleConversationType :
   {
   }
            break;
   case GroupConversationType:
   {
   }
            break;
}

位移枚举 ----- NS_OPTIONS

定义具有位移操作或特点的情况,使用枚举NS_OPTIONS(可复选的枚举)。

可复选枚举参考:Objective-C使用位运算设计可复选的枚举 位移枚举即是在你需要的地方可以同时存在多个枚举值如这样: (用“按位或操作符”可组合多个选项。用“|”来隔开)

 UISwipeGestureRecognizer *swipe_GR = [[UISwipeGestureRecognizer alloc] init];
swipe_GR.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;

例子:

typedef NS_OPTIONS(int,  ConversationType)
{
    ConversationNone  = 0,  // “= 0”可要可不要(默认从0开始)
    PeopleConversationType,
    GroupConversationType
} ;

或者:

typedef NS_OPTIONS(int,  ConversationType) {
    ConversationNone  = 0,
    PeopleConversationType = 1 << 0,
    GroupConversationType = 1 << 1
};

使用--枚举判断处理:

- (void)action:(ConversationType)type
{
    if (type == 0)
    {
        return;
    }

    if ((type & PeopleConversationType) == PeopleConversationType)
    {
        NSLog(@"个人");
    }

    if ((type & GroupConversationType) == GroupConversationType)
    {
        NSLog(@"群聊");
    }

}

调用:

ConversationType type = PeopleConversationType | GroupConversationType; 
[self action:type];



位运算:

按位与 --- "&" 只有对应的两个二进位均为1时,结果位才为1,否则为0

比如: 9&5,其实就是1001&0101=0001,因此9&5=1。 二进制中:与1相&就保持原位,与0相&就为0。

按位或 --- "|" 只要对应的二个二进位有一个为1时,结果位就为1,否则为0。

比如: 9|5,其实就是1001|0101=1101,因此9|5=13。

左移 --- << 把整数a的各二进位全部左移n位,高位丢弃,低位补0。左移n位其实就是乘以 2n次方

例如: 1<<3 就是0001左移3为1000,因此1<<2=8



晚安💤~ ~~ ~~~

2017.06.11

goyohol's essay