NS_ENUM和NS_OPTIONS有什么区别? [英] What is the difference between NS_ENUM and NS_OPTIONS?

查看:283
本文介绍了NS_ENUM和NS_OPTIONS有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Xcode5中用clang处理了下面的代码。

I preprocessed following code with clang in Xcode5.

typedef NS_ENUM(NSInteger, MyStyle) {
    MyStyleDefault,
    MyStyleCustom
};

typedef NS_OPTIONS(NSInteger, MyOption) {
    MyOption1 = 1 << 0,
    MyOption2 = 1 << 1,
};

得到这个。

typedef enum MyStyle : NSInteger MyStyle; enum MyStyle : NSInteger {
    MyStyleDefault,
    MyStyleCustom
};

typedef enum MyOption : NSInteger MyOption; enum MyOption : NSInteger {
    MyOption1 = 1 << 0,
    MyOption2 = 1 << 1,
};

我知道NS_OPTIONS是一个位掩码,但是有技术上的差异吗?
或者这只是命名约定吗?

I know NS_OPTIONS is for a bitmask, but is there any technical differences? Or this is just for naming convention?

EDIT

根据NS_OPTIONS的定义,它可能是为了编译器兼容性(特别是对于c ++编译器)

According to the definition of NS_OPTIONS, it's probably for compiler compatibility.(especially for c++ compiler)

// In CFAvailability.h
// Enums and Options
#if (__cplusplus && __cplusplus >= 201103L && (__has_extension(cxx_strong_enums) || __has_feature(objc_fixed_enum))) || (!__cplusplus && __has_feature(objc_fixed_enum))
  #define CF_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
  #if (__cplusplus)
    #define CF_OPTIONS(_type, _name) _type _name; enum : _type
  #else
    #define CF_OPTIONS(_type, _name) enum _name : _type _name; enum _name : _type
  #endif
#else
  #define CF_ENUM(_type, _name) _type _name; enum
  #define CF_OPTIONS(_type, _name) _type _name; enum
#endif

__ clang中的cplusplus值是199711,我无法测试。

__cplusplus value in clang is 199711 and I can't test what this is exactly for, though.

推荐答案

枚举和位掩码(选项)之间有一个基本区别。您使用枚举列出独占状态。在同时应用多个属性时,使用位掩码。

There's a basic difference between an enum and a bitmask (option). You use an enum to list exclusive states. A bitmask is used when several properties can apply at the same time.

在这两种情况下,都使用整数,但是它们有所不同。使用枚举查看数值,使用位掩码查看各个位。

In both cases you use integers, but you look at them differently. With an enum you look at the numerical value, with bitmasks you look at the individual bits.

typedef NS_ENUM(NSInteger, MyStyle) {
    MyStyleDefault,
    MyStyleCustom
};

只代表两种状态。

switch (style){
    case MyStyleDefault:
        // int is 0
    break;
    case MyStyleCustom:
        // int is 1
    break;
}

位掩码将代表更多的状态。

While the bitmask will represent more states. You check for the individual bits with logic or bitwise operators.

typedef NS_OPTIONS(NSInteger, MyOption) {
    MyOption1 = 1 << 0, // bits: 0001
    MyOption2 = 1 << 1, // bits: 0010
};

if (option & MyOption1){ // last bit is 1
    // bits are 0001 or 0011
}
if (option & MyOption2){ // second to last bit is 1
    // bits are 0010 or 0011
}
if ((option & MyOption1) && (option & MyOption2)){ // last two bits are 1
    // bits are 0011
}

tl; dr 枚举给出数字的名称。位掩码给出位的名称。

tl;dr An enum gives names to numbers. A bitmask gives names to bits.

这篇关于NS_ENUM和NS_OPTIONS有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆