Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C typedef enum in global constants file

OK, this is related to question "Constants in Objective C".

I created Constants.h and its corresponding Constants.m file:

// Constants.h
extern int const BOOKS; 
typedef enum SSDifficultyLevel {
    EASY = 0,
    MEDIUM = 1,
    HARD = 2
} SSDifficultyLevel;

// Constants.m
int const BOOKS = 66;

My question: Is OK for the enum to be typedef'd in Constants.h? The code is compiling fine (no warnings or errors so far) but I was wondering if this is the right way to do it, as the solution provided in the related question involves splitting the constant's definition and declaration.

Thanks.

like image 371
rtovars Avatar asked Oct 27 '11 06:10

rtovars


1 Answers

well, a constant and an enum serve different purposes (although there is some obvious overlap). so, just don't go too far from what people would expect, unless you have a really good reason to break that rule.

personally, i don't like the "global constants header" much, as you should usually associate those declarations with what they are used with. for example, Apple's frameworks typically declare the enums near the interfaces they relate to, and the notification names in the same header as the class.

other than that, you have declared things correctly.

if you use c++ or objc++, then you will want to fix that extern because the names may differ, and that can result in link errors.

something like this should do the trick:

#if defined(__cplusplus)
#define MONExternC extern "C"
#else
#define MONExternC extern
#endif

then you would declare BOOKS like so:

MONExternC int const BOOKS; 

one other note, and this may have been only for illustration in your example: those identifiers are very short, and can easily cause collisions with other identifiers.

like image 149
justin Avatar answered Oct 23 '22 13:10

justin