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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With