views:

250

answers:

3

Hi I have an enum declared like this:

typedef enum {
    Top,             
    Bottom,
    Center
} UIItemAlignment;

In my code I try to use it like this:

item.alignment = UIItemAlignment.Top;

I get an error like this: " Expected expression before 'UIItemAlignment' "

If I use only:

item.alignment = Top;

everything works fine but why do I get this error if I try to use it the other way?

_alignment is an NSInteger and it has a property declared like this

@property (readwrite) NSInteger alignment; and I synthesized it in my implementation file.

So my question is, why do I get this error?

+1  A: 

To specify an enumeration value you do not need to specify its type. To be more clear you could write something like this:

UIItemAlignment alignment = Top;
item.alignment = alignment;

But it is not necessary to do so.

fbrereto
+4  A: 

Enum values are not specified via their type in objective-c nor c++. The syntax you are trying to use is how C# handles it though.

Brian R. Bondy
A couple of additional thoughts: Consider using the type as a prefix for the enum values, e.g. ItemAlignmentTop, ItemAlignmentCenter, etc. Note that it's best to avoid using UI as a prefix, because that's what Apple uses as a prefix for everything in UIKit.
jlehr
+1  A: 

The values declared inside enum { } are integer constants in their own right and not tied to the enumeration type. They are used as int values similar to #define'd constants. Additionally you may choose to use the enum type (e.g. UIItemAlignment here) as a type of integer variable that is guaranteed to be able to represent the enumeration constants, but the type itself is not a class or a structure containing those constants - hence the . does not work.

Arkku