0

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.

2
  • USE_LOCALHOST, USE_LOCAL_HOST? Commented Sep 9, 2015 at 8:40
  • sorry, I just edited my post. Still the same question. Commented Sep 9, 2015 at 8:42

2 Answers 2

3

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.

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

1 Comment

Thanks, it seems to be a good way to share the preprocessor directive among files.
2

#define works only in the file where it's defined in. But you can #import "MyProgram.h" in MyWebService.h and problem solved. Every time you need to access USE_LOCALHOST, just import the header file.

3 Comments

I already imported the "MyProgram.h" right from the beginning in AppDelegate.m.
Not AppDelegate.m, but try to import it in MyWebService.h
Of course in that case, it will work. However, I don't want to do that. Each time, I need I have to give it a call to MyProgram.h. It's not the purpose of creating a global directive. Thanks to your response. Probably, my approach is wrong.

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.