objective-cxcodeconstants

Custom compile time variables for use in Objective-C code?


Is there a way to tell XCode to insert certain variables or constants into your project at compile time, for you to use in your code? Specifically, I'm talking about iOS projects, so I don't have the option to use command line arguments, I think.

Why would I need this, you ask? Well say that for certain cells in a table view, I'd like to add a different subtitle text, because it helps me pick the right cell during development. Something like this would be awesome:

if (MY_COMPILE_TIME_BOOL_CONST) {
    cell.subtitle.text = [NSString stringWithFormat:@"Contains %i items", count];
} else {
    cell.subtitle.text = @"";
}

But there are lots of other places this would come in handy. Somehow, when trying to google the answer, I only get to "beginning with XCode tutorials".

Can anyone point towards the magic?

UPDATE: The magic word to google for is "preprocessor macro". Thanks, SO!


Solution

  • If you start your app from XCode you can totally use command line arguments.

    You could also add something like DEBUG=1 to the preprocessor macro for the debug configuration. Then you can add preprocessor ifs into your code and the corresponding code will only be compiled in Debug mode.

    #if DEBUG
    NSLog(@"Foo");
    #endif
    

    The latter is what I use every day.