
当如此声明枚举时,枚举常量的数据类型如何保证为NSUInteger而不是unsigned int:
enum { NSNullCellType = 0,NSTextCellType = 1,NSImageCellType = 2};typedef NSUInteger NSCellType; NSUInteger的typedef似乎没有以任何方式绑定到枚举声明。
完整版
我通过苹果的64-Bit Transition Guide for Cocoa阅读一些关于枚举值的指导,我来了一个问题。这里是一个(冗长的)报价从枚举常量部分,强调我的:
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; 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.
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,NSImageCellType = 2} NSCellType; there is Now this:
enum { NSNullCellType = 0,NSImageCellType = 2};typedef NSUInteger NSCellType; 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?
解决方法 现在有NS_ENUM启动Xcode 4.5:typedef NS_ENUM(NSUInteger,NSCellType) { NSNullCellType = 0,NSImageCellType = 2}; 如果你使用二进制标志,你可以考虑NS_OPTIONS:
typedef NS_OPTIONS(NSUInteger,MyCellFlag) { MyTextCellFlag = 1 << 0,MyImageCellFlag = 1 << 1,}; 总结 以上是内存溢出为你收集整理的目标c枚举值:NSInteger或int?全部内容,希望文章能够帮你解决目标c枚举值:NSInteger或int?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)