开发者

Objective-C enum error?

I've defined an enum at the top of my class header:

enum PresetSeq{kSeqClear=0, kSeqAllBeats, kSeqAllTicks};

But when I try to declare a method for that class using the enum.

-(void)quickSetSeq:(PresetSeq)pattern  forChannel:(long)ch;

I get the error: expected ')' before 'PresetSeq'

If I typedef the enum instead:

开发者_如何学运维typedef enum  {kSeqClear=0, kSeqAllBeats, kSeqAllTicks} PresetSeq;

Then the compiler is happy, but I don't remember having to do that in C/C++.

Do enums have to be typedef's in Obj-C?

Thanks

Steve

P.S. - I saw other posts about Obj-C enums, but not why this simple case fails.


These are C enums. (Remember Objective-C is just a strict superset of C).

To define an enumerated type, you must use typedef.

However, if you do not need to have a standalone type (without the enum prefix) to collectively refer to that enum, then you do not need typedef and you can just declare it like this:

enum PresetSeq {
  kSeqClear, 
  kSeqAllBeats, 
  kSeqAllTicks
};

So, in your case, you can do it either way:

typedef enum {
  kSeqClear,
  kSeqAllBeats,
  kSeqAllTicks
} PresetSeq;

-(void)quickSetSeq:(PresetSeq)pattern  forChannel:(long)ch;

Or without typedef and using the enum PresetSeq syntax as shown above:

-(void)quickSetSeq:(enum PresetSeq)pattern  forChannel:(long)ch;


If you use:

enum  PresetSeq {kSeqClear=0, kSeqAllBeats, kSeqAllTicks};

then you must use the enum name:

-(void)quickSetSeq:(enum PresetSeq)pattern  forChannel:(long)ch;

Your initial error is because there is no PresetSeq type, just a enum PresetSeq one.

When you do the typedef version, that creates a type alias PresetSeq that you can use.

It's exactly the same as:

struct X {int a;};
typedef struct (int a;} Y;

In that case, you can use struct X or Y as a type but you cannot use X on its own.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜