1

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!

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

See, I was googling "compile time constants" where what I was actually looking for was "preprocessor macro". This works exactly the way I want it to. Thanks!
2

If you do not want to add one or more header files for these macros try the steps below.

In Xcode, double click the target, select the Configuration (Debug/Release/etc) you want your special defines in. Then type preprocessor" in the search field. Then you should see Preprocessor Macros where you can enter your extras.

Comments

0

what about :

#define DEBUG YES

in your .pch

then you could do something like

if (DEBUG) {
   cell.subtitle.text = @"Counting xyz";
   return cell;
}

cell.subtitle.text = @"";
return cell;

if youre mainly developing in simulator you could also check for simulator.

1 Comment

Ah yes, that was my first idea too, but I'm working in a team and would like to link the constants to the active configuration (debug/release/distribution)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.