epologee
epologee

Reputation: 11319

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!

Upvotes: 1

Views: 2158

Answers (3)

Matthias Bauch
Matthias Bauch

Reputation: 90117

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.

Upvotes: 3

epatel
epatel

Reputation: 46041

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.

Upvotes: 2

choise
choise

Reputation: 25244

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.

Upvotes: 0

Related Questions