枚举类型

作者: 背包技术 | 来源:发表于2019-10-08 09:51 被阅读0次

真理惟一可靠的标准就是永远自相符合。

枚举的作用和定义

枚举的作用在于规范并语义化的定义代码中的状态、选项等常量。如果一个变量只有几种可能的值,比如屏幕支持的方向只有上(Orientation Portrait)、下(Orientation Upside down)、左(Orientation Landscape Left)、右(Orientation Landscape Right)。就可以使用枚举类型来定义一个代表方向枚举类型变量。

枚举类型的定义以关键字enum开头,之后是枚举数据类型的名称,最后是在一对花括号内的选项标识符序列。 (实现枚举所用的数据类型取决于编译器。除了使用默认类型,还可以指明用何种“底层数据类型”来保存枚举类型的变量):

//使用编译器默认类型
enum Orientation {
        Orientation_Portrait,
        Orientation_Down,
        Orientation_left,
        Orientation_Right
    };
//指明枚举类型保存变量的类型
enum Orientation: NSUInteger {
        Orientation_Portrait,
        Orientation_Down,
        Orientation_left,
        Orientation_Right
    };
//定义匿名枚举类型,并定义两个枚举变量
enum {
        Orientation_Portrait,
        Orientation_Down,
        Orientation_left,
        Orientation_Right
    } WX_Orientation,QQ_Orientation;

枚举类型值的分配原则:
编译器会为枚举分配一个独有的编号(枚举变量的本质就是无符号整数(%u占位符)),从0开始,每个枚举递增1。(可以手动指定某个枚举成员所对应的值,接下来的枚举值会在上一个的基础上递增1)。

使用关键字typedef重新定义枚举,目的是为了简化枚举的声明,不需要每次都写enum

enum Orientation: NSUInteger {
        Orientation_Portrait,
        Orientation_Down,
        Orientation_left,
        Orientation_Right
    }; 
typedef enum Orientation Orientation;
//使用Orientation代替完整的enum Orientation来声明两个枚举变量
Orientation ori_1, ori_2;

合并后

typedef enum Orientation: NSUInteger {
    Orientation_Portrait,
    Orientation_Down,
    Orientation_left,
    Orientation_Right
} Orientation;
基本使用方法

//定义枚举
typedef enum Orientation: NSUInteger {
    Orientation_Portrait,
    Orientation_Down,
    Orientation_left,
    Orientation_Right
} Orientation;
//创建枚举变量并赋值
Orientation QQ_Orientation = Orientation_Portrait;

//调用函数
 [self defineQQOrientation:QQ_Orientation];

- (void)defineQQOrientation:(Orientation)orientation{
    switch (orientation) {
        case Orientation_Portrait:
            break;
        case Orientation_Down:
            break;
        case Orientation_left:
            break;
        case Orientation_Right:
            break;
            
        default:
            break;
    }
}

枚举的另一种使用方式是定义为按位掩码,当定义选项的时候,若这些选项可以彼此组合,则在设置特定的枚举值后,各选项间就可通过“按位或”来组合。因为每个枚举值所对应的二进制表示中,只有1个二进制位的值是1,所以多个选项“按位或”后的组合值是唯一的,且将某一选项与组合值做“按位与”操作,即可判断出组合值中是否包含该选项。

enum Orientation {
        Orientation_Portrait = 1 << 0, // 0001
        Orientation_Down = 1 << 1,// 0010
        Orientation_left = 1 << 2,// 0100
        Orientation_Right = 1 << 3// 1000
    };

多选项使用方法:

typedef enum OrientationGroup {
    OrientationGroup_Portrait = 1 << 0, // 0001,
    OrientationGroup_Down = 1 << 1, // 0010,
    OrientationGroup_left = 1 << 2, // 0100,
    OrientationGroup_Right = 1 << 3, // 1000
} OrientationGroup;

//用“或”运算同时赋值多个选项
OrientationGroup orientationGroup = OrientationGroup_left | OrientationGroup_Down;
    
//用“与”运算取出对应位
if (orientationGroup & OrientationGroup_Portrait) {
    NSLog(@"OrientationGroup_Portrait OK");
}
    
if (orientationGroup & OrientationGroup_left) {
    NSLog(@"OrientationGroup_left OK");
}
    
if (orientationGroup & OrientationGroup_Right) {
    NSLog(@"OrientationGroup_Right OK");
}
    
if (orientationGroup & OrientationGroup_Down) {
    NSLog(@"OrientationGroup_Down OK");
}

运行结果:

2019-09-19 14:17:52.610619+0800 Text[2143:121039] OrientationGroup_left OK
2019-09-19 14:17:52.610763+0800 Text[2143:121039] OrientationGroup_Down OK
enum在Objective-C中的“升级版”

从C++ 11开始,我们可以为枚举指定其实际的存储类型,在枚举的定义中已经提到过,如下

//指明枚举类型保存变量的类型
enum Orientation: NSUInteger {
        Orientation_Portrait,
        Orientation_Down,
        Orientation_left,
        Orientation_Right
    };

在Objective-C中为了保证枚举类型的兼容性,推荐使用NS_ENUM和NS_OPTIONS

// NS_ENUM,定义状态等普通枚举类型
typedef NS_ENUM(NSInteger, NS_Orientation) {
    NS_Orientation_Portrait,
    NS_Orientation_Down,
    NS_Orientation_left,
    NS_Orientation_Right
};
// NS_OPTIONS,定义可组合选项的枚举类型
typedef NS_OPTIONS(NSInteger, OP_Orientation) {
    OP_Orientation_None = 0,
    OP_Orientation_Portrait = 1 << 0,
    OP_Orientation_Down = 1 << 1,
    OP_Orientation_left = 1 << 2,
    OP_Orientation_Right = 1 << 3
};

相关文章

  • C语言基础 之 枚举类型

    枚举类型 枚举类型: 列出所有可能的值 枚举类型的定义 枚举类型定义的一般格式:enum 枚举类型名 {枚举值表}...

  • Swift 基础笔记 - 枚举

    枚举 OC定义和使用枚举 Swift定义枚举类型 Swift判断枚举类型 枚举成员类型

  • 枚举

    枚举 枚举就是专门用来表示几种固定类型的取值 枚举的本质就是基本数据类型,整型 枚举类型定义格式 定义枚举类型变量...

  • JavaScript中的Enum枚举类型数据

    一、枚举类型介绍 1、枚举类型 如果接触过其它语言或者TypeScript,大概对于枚举类型有一些了解。枚举类型是...

  • TS学习笔记(6)-枚举类型

    枚举类型 ========= 知识点 枚举类型的定义方法 枚举类型的使用方法 代码

  • WWDC2015Session106What's New

    新特性:基本类型、类型匹配、可检查性、协议扩展、错误处理 基本类型 枚举 在枚举中加入类型: 递归枚举递归枚举中需...

  • 枚举类

    1.枚举类型的定义: 枚举类型定义的一般形式为 enum 枚举名{//枚举值表枚举值1;枚举值2;...} 在枚举...

  • C语言学习 - 枚举型数据类型

    枚举变量的定义 方式一Step 1:声明枚举类型:格式:enum 枚举类型名{枚举值1,...,枚举值n}如:en...

  • typescript语法精讲四(笔记)

    - 枚举类型 枚举的特性就是将一组可能出现的值,列举出来,定义到类型中去 - 枚举类型的值 枚举类型默认是有值的,...

  • C++学习笔记(二)

    1 数据类型 枚举类型枚举类型枚举类型(enumeration)是C++中的一种派生数据类型,它是由用户定义的若干...

网友评论

    本文标题:枚举类型

    本文链接:https://www.haomeiwen.com/subject/vpfluctx.html