views:

77

answers:

2

This is just out of curiosity but when i declare an enum type, would it be better to have it within an implementation declaration or outside of it? What would be best practice? For example:

@implementation PostQuestionScene

enum popUpItems{ 
 kExpiredBox, 
 kPauseBackground
};

vs..

enum popUpItems{ 
 kExpiredBox,
};

@implementation PostQuestionScene ..
+2  A: 

I don't think it makes any technical difference. I would place it before the @implementation (along with all other miscellaneous declarations) unless it is just used in one or a small group of methods, in which case I would place it immediately before those methods.

Of course, if it might be used by clients or subclasses it should be in your header file (where @interface is) so that the definition is visible to them.

Kevin Reid
+2  A: 

I tend to always have a typedef so it's just like another variable:

#typedef enum { 
 kExpiredBox, 
 kPauseBackground
} popUpItems_t;

Then create instances of it.

popUpItems_t popUpItems;

If you will use it outside that module, put the typedef in the header so when the header is included, other modules have the typedef at their disposal (if they need to take it as an argument, for example,) otherwise put it in the implementation (think public/private variables.)

Jeff Lamb
What’s with the `#typedef enum`? Don’t you just mean `typedef enum`?
Jonathan Sterling
Yep. Was wondering when someone would notice. +1 to you.
Jeff Lamb