简体   繁体   English

枚举不是由编译器检查的类型?

[英]enum Not Type Checked by Compiler?

With these enums... 有了这些枚举......

typedef enum {
    ThisThingA = 0,
    ThisThingB = 1
} ThisThing;

typedef enum {
    ThatThingX = 8,
    ThatThingY = 9
} ThatThing;

and these properties... 和这些属性......

@property (nonatomic) ThisThing thisThing;
@property (nonatomic) ThatThing thatThing;

I can do this... 我可以做这个...

self.thisThing = thatThingX;

and I don't get a warning from the compiler, which I would expect. 而且我没有得到编译器的警告,我希望如此。 Why is there no warning from the compiler? 为什么编译器没有警告? Why can I assign something that is of type ThatThing to something that is of type ThisThing? 为什么我可以将类型为ThatThing的东西分配给ThisThing类型的东西?

EDIT as per the answer from Martin R: But if I do this... 根据Martin R的回答编辑:但如果我这样做......

[self setThisThing:thatThingX];

I get the warning: Implicit conversion from enumeration type 'ThatThing' to different enumeration type 'ThisThing' 我收到警告:从枚举类型'ThatThing'到不同的枚举类型'ThisThing'的隐式转换

(Xcode 4.6.3 and iOS 6.0) (Xcode 4.6.3和iOS 6.0)

The compiler option "Implicit Enum Conversions (-Wenum-conversion)" is by default on, and you actually get a warning if you assign to a variable of different enum type: 默认情况下,编译器选项“Implicit Enum Conversions(-Wenum-conversion)”处于启用状态,如果您指定了不同枚举类型的变量,则实际上会收到警告:

ThisThing x = ThatThingX;

or if you use the setter method to set the property: 或者如果使用setter方法设置属性:

[self setThisThing:ThatThingX];

In both cases you get the warning 在这两种情况下,您都会收到警告

implicit conversion from enumeration type 'ThatThing' to different enumeration type 'ThisThing' [-Wenum-conversion]

Only when you use the "dot" syntax to set the value 仅当您使用“点”语法设置值时

self.thisThing = ThatThingX;

then you don't get a warning, so this could be a bug in the compiler. 然后你没有得到警告,所以这可能是编译器中的一个错误。

enum is C construct that is an integer type. enum是C构造,它是一个整数类型。 It is type checked. 它是类型检查。 Your usage is the same default integer type so there is nothing wrong in the eyes of the compiler or at all. 您的用法是相同的默认整数类型,因此在编译器的眼睛中根本没有任何错误。 Your usage is int. 你的用法是int。

If you typedef int as Bob and again as Josephine, that is a convenience for code clarity. 如果你将typedef int作为Bob并再次作为Josephine,这样可以方便代码清晰度。 Bob and Josephine are still the same type int. 鲍勃和约瑟芬仍然是同一类型int。

If you declare two enums but one uses NSUInteger and the other uses NSInteger you could get a warning depending on your settings for implicit conversion. 如果您声明两个枚举但一个使用NSUInteger而另一个使用NSInteger,则可能会收到警告,具体取决于隐式转换的设置。

This is really a C question. 这真的是一个C问题。

Actually enum options are treated as NSInteger. 实际上,枚举选项被视为NSInteger。 So thatThingX has the value of 0 by default. 因此,默认情况下, theThingX的值为0。 When you assign self.thisThing = thatThingX; 当你分配self.thisThing = thatThingX; self.thisThing got the value of 0. So it will become ThisThingA . self.thisThing的值为0.所以它将成为ThisThingA That's why there are no warnings. 这就是没有警告的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM