简体   繁体   中英

Conditional Compilation and Objective-C/Xcode

So I am working on a learning project and I am trying to create a header file that contains a store of URL's so that you can just change a single flag to change from Debug to Production. This is what I am trying to do with the compiler and it is clearly wrong. I can't find any information on how to do this in Objective-C, so that's why I came here.

#define DEBUG 1
#if DEBUG
  NSString *URL = @"dev.myserver.com";
#else
  NSString *URL = @"myserver.com";
#endif

@interface GlobalURLRefrences : NSObject {
  //NSString *URL; removed this
}

//@property (nonatomic, retain) NSString *URL; removed this

@end

Now I am not sure if I need to declare that as a property or not. Also, once this is compiled properly, how to I access it from an outside class (of course after you #import the globalURL's class) Any sort of guidance on the proper method of doing this would be greatly appreciated.

Geoff: I have a need for this kind of conditional in my Mac App Store app for validating receipts, and I do it with a separate build configuration and a -D flag. In Debug configuration, add a compiler flag something like -DDEBUG_BUILD (Note the double D at the beginning and no space.) and then use

#ifdef DEBUG_BUILD
    #define SERVER_URL_STRING @"http://dev.myserver.com"
#else
    #define SERVER_URL_STRING @"http://myserver.com"
#endif

This way, you don't have to remember to swap that #define each time you build for production. (You'll forget eventually. Everyone has.) -If you do it this way, then you don't need the @property or the ivar declaration either.- Just saw you took these out already.

I think this should work

#define DEBUG 1
#if DEBUG
   #define URL = @"dev.myserver.com";
#else
   #define URL = @"myserver.com";
#endif

I think you are trying to define the same variable twice. How about this:

In your header:

#define DEBUG 1

In the init of your .m file:

#if DEBUG
   URL = @"dev.myserver.com";
#else
   URL = @"myserver.com";
#endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM