3

been researching this a while and not sure entirely what to do.

I want to allow users to switch debug mode either on or off. With debug mode on NSLogs will be printed to console.

currently I can set debug mode on or off in the build settings using a preprocessor (DEBUG) and I use the following code to "block" NSLogs.

#ifdef DEBUG
    NSLog(@"If you can see this then debug is on");
#endif

I have created a toggle switch in the settings page to get input from the user but I don't know how to use this input to then undefined/redefine DEBUG. Any ideas?

I am not sure if this is even possible so any alternate solutions would also be appreciated.

Many Thanks :)

IainNotAndrew
  • 136
  • 1
  • 2
  • 10

2 Answers2

6

You should not use preprocessor directives: using #ifdef DEBUG means that, if DEBUG is not defined, that piece of code doesn't get compiled at all.

You should instead replace preprocessor directives with a simple if statement that check a global variable (or, at least, that may be a solution).

Manlio
  • 10,768
  • 9
  • 50
  • 79
  • Hey there. This may be a completely different question but should I use a global variable or a singleton? Thanks again :) – IainNotAndrew Sep 13 '11 at 15:45
  • Not sure if there's a "right" choice, however I'd use a simple variable since it seems the simplest solution. You may think to a singleton if the number of global variables becomes to high or if you need to do additional checks, rather than a simple `if(DEBUG)`. Again, that's just my opinion, I'm not sure that's the best thing to do. :) – Manlio Sep 13 '11 at 17:13
0

I believe your code block would only check if you are building for debug or release and will build accordingly.

You can build it on a device it will be on release mode , I don't think it is possible to run the simulator in release mode otherwise.

Maybe manually building the application for simulator and moving the packed file to run only on simulator without running xcode, but it will not be reasonable I guess.

Ugur Kumru
  • 986
  • 6
  • 9