iOS框架类中的c enum定义

时间:2012-02-13 17:20:33

标签: objective-c c enums foundation

我正在浏览UIKit框架头文件,我看到很多实例,其中定义了匿名枚举,然后是看似相关的typedef。有人能解释一下这里发生了什么吗?

UIViewAutoresizing类型以某种方式(隐式)是否引用前一语句中声明的枚举?你会如何引用这种枚举类型?

enum {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
typedef NSUInteger UIViewAutoresizing;

2 个答案:

答案 0 :(得分:3)

我认为你在这里只弄错了一句:NSUInteger不是一个客观的c对象,它在32位系统上是unsigned int而在64位上是unsigned long比特系统。实际上这就是正在发生的事情:

typedef unsigned int UIViewAutoresizing;

typedef unsigned long UIViewAutoresizing;

如需更多参考,请添加:

#if __LP64__ || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif

来源:CocoaDev

答案 1 :(得分:3)

问题是那些是用作位掩码的标志,这会导致枚举问题。例如,如果它看起来像这样:

typedef enum {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
} UIViewAutoresizing;

你会在setAutoresizingMask:的视图上调用UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight,编译器会抱怨,你必须明确地将它强制转换回UIViewAutoresizing类型。然而,NSUInteger可以采用位掩码。

除此之外,lef2所说的关于NSUInteger的所有内容都不是ObjC对象。