Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using enums as parameters in an external file in Objective-C?

I have an enum named RandomEnum in file foo.h:

// foo.h
typedef enum RandomEnum {
  ran_1 = 0,
  ran_2
} RandomEnum;

In another file, bar.h, I'm trying to use RandomEnum as a parameter type:

// bar.h
#import "foo.h"

@interface bar : NSObject {}
  -(RandomEnum)echo:(RandomEnum)ran;
@end

However, the compiler doesn't seem to recognize RandomEnum. Is doing this even possible?

Compiler Error:

error: expected ')' before 'RandomEnum'

Edit: Added code for foo.h for clarification

like image 782
soundly_typed Avatar asked Dec 29 '09 21:12

soundly_typed


3 Answers

The C construct enum RandomEnum does not define a type called RandomEnum — it defines a type called enum RandomEnum. To be able to write just RandomEnum for the type, you need to use a typedef.

like image 159
Chuck Avatar answered Oct 12 '22 18:10

Chuck


It turns out this is possible after all. My problem had to do with odd cross-includes that weren't direct, but were still present.

In the given example, foo.h included thing.h which included something.h which included bar.h. This cross dependency is what ended up being the problem.

Still, good to know for compiler bugs. Thanks for the responses!

like image 22
soundly_typed Avatar answered Oct 12 '22 16:10

soundly_typed


As @Chuck said, it will work if you do this if you don't want to declare a typedef:

-(RandomEnum)echo:(enum RandomEnum)ran;
like image 33
Brock Woolf Avatar answered Oct 12 '22 16:10

Brock Woolf