chipbk10 chipbk10 - 4 months ago 9
Objective-C Question

#define a global preprocessor directive in a separate file in Objective-C

I want to define a global preprocessor directive in my app. For example:

In MyProgram.h, I define:

#define USE_LOCALHOST


I import this file from beginning in appDelegate.m.

For the later use, in another file, I refer to this global preprocessor directive.

In MyWebService.h, I define:

#ifdef USE_LOCALHOST
static NSString *MY_SERVER = @"http://192.168.1.130:8888";
#else
static NSString *MY_SERVER = @"http://myserver.com";
#endif


However, the value of
MY_SERVER
is always @"http://myserver.com". How to make it work properly? Thanks.

Answer

Define it in .pch. And you will never forget to include .h-file where you have defined USE_LOCALHOST.

Or you can define it in build settings in Preprocessor Macros. For example only for Debug.