2

QUESTION

I would like to trigger a warning only if my TESTING is YES. Is this possible? What I have now doesn't work. What should I do?

BOOL const TESTING = NO;
#if TESTING == YES
    #warning don't forget to turn testing to NO before upload
#endif

ANSWER

Based on the answer below, here is what worked for me:

#define _TESTING // COMMENT THIS OUT TO DISABLE TESTING MODE
#ifdef _TESTING
    BOOL const TESTING = YES;
    #warning don't forget to turn testing to NO for production
#else
    BOOL const TESTING = NO;
#endif
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
  • 2
    Yeah, the program and macro name spaces are entirely different. Setting the program value TESTING to NO has no effect on the value of the macro variable TESTING (though the value of the macro variable is substituted the other direction, in a text replacement pass before the actual compiler even sees the program). – Hot Licks Feb 15 '13 at 17:11
  • @HotLicks: So if he's using his `TESTING` bool in the program to say, show an alert view or something, he'll still need to keep the `BOOL const TESTING = NO;` to make the program code recognize it, correct? So he will have to both comment out the `#define TESTING` and set `BOOL const TESTING = NO` when he releases, correct? – GeneralMike Feb 15 '13 at 17:15
  • He should not name his BOOL variable TESTING, or the macro phase will replace it with NO, resulting in some mysterious compilation errors. Thus, always use all UPPER-CASE for macro variables, and never use all upper case for program variables. But he can have the macro variable TESTING control the setting of his Testing variable, with ifdefs or other techniques. – Hot Licks Feb 15 '13 at 18:51
  • Note that, as stated just above, it's a bad idea to have an all UPPER-CASE program variable name. – Hot Licks Feb 15 '13 at 19:27
  • Your answer is perfect, Jackson. Thanks. – Marcelo dos Santos Aug 11 '15 at 17:22

1 Answers1

2

Try replacing what you have with

#ifdef TESTING
  #warning //... warning here
  BOOL const testingBool = YES;
#else
  BOOL const testingBool = NO;
#endif

Then, you need to add TESTING as a "Preprocessor Macro" in your Target's Build Settings (see this question for more details on how to do that).

Community
  • 1
  • 1
GeneralMike
  • 2,951
  • 3
  • 28
  • 56