Steve555
Steve555

Reputation: 203

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.

Upvotes: 6

Views: 4342

Answers (2)

paxdiablo
paxdiablo

Reputation: 881423

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.

Upvotes: 4

Jacob Relkin
Jacob Relkin

Reputation: 163238

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;

Upvotes: 6

Related Questions