枚举值:NSInteger或int? [英] enum values: NSInteger or int?

查看:186
本文介绍了枚举值:NSInteger或int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当声明枚举时,枚举常量的数据类型如何保证为NSUInteger而不是unsigned int:

How are the data types of an enum's constants guaranteed to be NSUInteger instead of unsigned int when declaring an enum thusly:

enum {
    NSNullCellType = 0,
    NSTextCellType = 1,
    NSImageCellType = 2
};
typedef NSUInteger NSCellType;

NSUInteger的typedef似乎不以任何方式绑定到枚举声明。

The typedef to NSUInteger does not appear to be tied to the enum declaration in any way.

我正在阅读Apple的 针对Cocoa的64位过渡指南 有关枚举值的一些指导,我提出了一个问题。以下是枚举常量部分中的(冗长的)报价:

I was reading through Apple's 64-Bit Transition Guide for Cocoa for some guidance on enum values and I came away with a question. Here's a (lengthy) quote from the Enumeration Constants section, emphasis mine:


枚举(枚举)常量是它们的数据类型经常是不确定的。换句话说,枚举常量不是可预测的unsigned int。对于常规构造的枚举,编译器实际上根据查找的内容设置底层类型。底层类型可以是(signed)int或甚至long。请看下面的示例:

A problem with enumeration (enum) constants is that their data types are frequently indeterminate. In other words, enum constants are not predictably unsigned int. With conventionally constructed enumerations, the compiler actually sets the underlying type based on what it finds. The underlying type can be (signed) int or even long. Take the following example:



type enum {
    MyFlagError = -1,
    MyFlagLow = 0,
    MyFlagMiddle = 1,
    MyFlagHigh = 2
} MyFlagType;




编译器查看此声明,一个成员常量,声明枚举int的底层类型。如果成员的值的范围不适合int或unsigned int,则基类型将静默地变为64位(长)。因此,定义为枚举的数量的基本类型可以静默地更改大小以与枚举中的值一致。无论您是编译32位还是64位,都可能发生这种情况。不用说,这种情况给二进制兼容性带来了障碍。

The compiler looks at this declaration and, finding a negative value assigned to one of the member constants, declares the underlying type of the enumeration int. If the range of values for the members does not fit into an int or unsigned int, then the base type silently becomes 64-bit (long). The base type of quantities defined as enumerations can thus change silently size to accord with the values in the enumeration. This can happen whether you're compiling 32-bit or 64-bit. Needless to say, this situation presents obstacles for binary compatibility.

作为对这个问题的补救,苹果决定更明确的枚举类型在Cocoa API 而不是在枚举方面声明参数,头文件现在单独声明一个枚举类型,其大小可以指定。枚举的成员及其值如前所述进行声明和分配。例如,代替:

As a remedy for this problem, Apple has decided to be more explicit about the enumeration type in the Cocoa API. Instead of declaring arguments in terms of the enumeration, the header files now separately declare a type for the enumeration whose size can be specified. The members of the enumeration and their values are declared and assigned as before. For example, instead of this:



typedef enum {
    NSNullCellType = 0,
    NSTextCellType = 1,
    NSImageCellType = 2
} NSCellType;




现在有:

there is now this:



enum {
    NSNullCellType = 0,
    NSTextCellType = 1,
    NSImageCellType = 2
};
typedef NSUInteger NSCellType;




枚举类型根据NSInteger或NSUInteger定义,基本枚举类型64位在64位体系结构上。

The enumeration type is defined in terms of NSInteger or NSUInteger to make the base enumeration type 64-bit capable on 64-bit architectures.

我的问题是这是因为typedef不会出现明确地绑定到枚举声明,如何知道他们的数据类型是否是unsigned int或NSUInteger?

My question is this: given that the typedef doesn't appear to be tied explicitly to the enum declaration, how does one know if their data types are unsigned int or NSUInteger?

推荐答案

NS_ENUM 启动Xcode 4.5:

There is now NS_ENUM starting Xcode 4.5:

typedef NS_ENUM(NSUInteger, NSCellType) {
    NSNullCellType = 0,
    NSTextCellType = 1,
    NSImageCellType = 2
};

如果你工作,你可以考虑 NS_OPTIONS 与二进制标志:

And you can consider NS_OPTIONS if you work with binary flags:

typedef NS_OPTIONS(NSUInteger, MyCellFlag) {
    MyTextCellFlag = 1 << 0,
    MyImageCellFlag = 1 << 1,
};

这篇关于枚举值:NSInteger或int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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