2

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?

Community
  • 1
  • 1
Aaron Vegh
  • 5,217
  • 7
  • 48
  • 75
  • 2
    Just checking. You do know this will be a compile time conditional, not a run time choice. If you are building a universal app this will not do what you want it to do. – Justin808 Nov 22 '12 at 00:26
  • Is this a universal app and you want the two constants to have different values at runtime based on the device the app is currently running on? Or is this shared code used to build separate iPad and iPhone apps? – rmaddy Nov 22 '12 at 00:47
  • Is your `IS_IPAD` macro defined as `UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad`? – rmaddy Nov 22 '12 at 00:48
  • @maddy -- yes. I knew this had something to do with availability at runtime. Which is when I want it set! Dasblinkenlight below modifies his answer to give what I'm after. Thanks! – Aaron Vegh Nov 22 '12 at 02:05

2 Answers2

4

The if statement of the C language cannot be used outside a function body to control a global declaration like that. You can use the preprocessor's #if instead:

#if (IS_IPAD)
    integer_t const kFontSize = 25;
    integer_t const kFontSizeMicro = 15;
#else
    integer_t const kFontSize = 20;
    integer_t const kFontSizeMicro = 12;
#endif

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 drop const from the declarations, and put the regular if into an initialization function - for example, the application:didFinishLaunchingWithOptions:.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • My read of the question is that this is a universal iOS app and he wants to set the values of the constants at runtime based on the current device type. – rmaddy Nov 22 '12 at 00:49
  • @rmaddy I think you are right - if it weren't for the desire to set these at runtime, the OP would be fine with `#define`s. I edited the answer to describe a path to making constants settable at runtime. Thanks! – Sergey Kalinichenko Nov 22 '12 at 00:58
3

you could also use ternary operator which would give you something like this:

integer_t const kFontSize = (IS_IPAD)? 25 : 20;
integer_t const kFontSizeMicro = (IS_IPAD)? 15 : 12;

it's juste like a if(IS_IPAD){ kFontSize = 25 }else{ kFontsize = 20} declaration

if you use your constant in calculations, you might need to put it between (), else you might have some warnings because of operation priority problems.

so something like that : (kFontSize)*2+1

Saliom
  • 1,050
  • 9
  • 15