Objective-C typedef enum in global constant file

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

I created Constants.h and the 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: is it normal for enum be typedef 'd in Constants.h ? The code compiles in the order (no warnings or errors so far), but I was wondering if this was the right thing to do, since the solution presented in the related question involves splitting the definition and declaring the constant.

Thanks.

+7
source share
1 answer

Well, constant and enumeration serve different purposes (although there is some obvious coincidence). so just don’t go too far from what people expected unless you have really good reason to break this rule.

I personally don’t like the heading "global constants", since you usually associate these ads with what they are used with. for example, the Apple infrastructure typically declares enumerations next to the interfaces to which they relate, and notification names in the same header as the class.

besides that, you have correctly declared yourself.

if you are using C ++ or objC ++ then you will want to fix it extern because the names may be different and this can lead to link errors.

something like this should do the trick:

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

then you declare BOOKS as follows:

 MONExternC int const BOOKS; 

one more note, and this can only be illustrative in your example: these identifiers are very short and can easily cause conflicts with other identifiers.

+3
source

All Articles