Oliver
Oliver

Reputation: 403

Obj-C: Typedefs across classes?

I want to typedef an enum that is common across several classes. I could:

Whilst the first option requires no repetition -- and I think this is the best of the three -- it just feels wrong creating a whole new class just for just one enum. Is this anti-pattern? Is there a better way?

Upvotes: 3

Views: 368

Answers (1)

IluTov
IluTov

Reputation: 6862

Usually done like this:

#ifndef SomeEnum_enum
#define SomeEnum_enum

typedef enum {
    SomeEnumOne,
    SomeEnumTwo,
    SomeEnumThree
} SomeEnum;

#endif

You have to put your enum in your .h file, it can be one of a class, a separate file just for enums, a category, it really doesn't matter.

Now you can put it wherever you want, you just have to include the .h file

Upvotes: 4

Related Questions