Obj-C Error: Expected expression before ...... (why?)
Hi I have an enum declared like this:
typedef enum {
Top, 开发者_StackOverflow社区
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?
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.
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.
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.
精彩评论