I have set up some constants in my Xcode project using the advice in this question, and it works just fine. Now, however, I would like to set a different value for two of these constants depending on whether it’s an iPad or iPhone.
Here’s my Constants.h file:
extern integer_t const kFontSize;
extern integer_t const kFontSizeMicro;
and my .m:
integer_t const kFontSize = 16;
integer_t const kFontSizeMicro = 11;
Now I would like to change those values depending on a #define macro setup in my .pch file. But this doesn’t work:
if (IS_IPAD) {
integer_t const kFontSize = 25;
integer_t const kFontSizeMicro = 15;
}
The error I get is “Expected identifier or (” on the “if”.
Anyone know how to allow the conditional statement in this case?
The
ifstatement of the C language cannot be used outside a function body to control a global declaration like that. You can use the preprocessor’s#ifinstead:This is not too different from using
#define-d constants, though. If you would like to make the constants settable at runtime, – say, during initialization, – you will need to dropconstfrom the declarations, and put the regularifinto an initialization function – for example, theapplication:didFinishLaunchingWithOptions:.