Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obj-C: Typedefs across classes?

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

  • 1 Make the typedef in a class that the others inherit from directly

  • 2 Typedef the enum for each of the classes.

  • 3 Create a category for NSObject and typedef the enum there.

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?

like image 928
Oliver Avatar asked Apr 07 '26 20:04

Oliver


1 Answers

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

like image 173
IluTov Avatar answered Apr 09 '26 18:04

IluTov