Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 570
Steve555 Avatar asked Nov 04 '10 06:11

Steve555


1 Answers

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;
like image 186
Jacob Relkin Avatar answered Sep 26 '22 16:09

Jacob Relkin