2

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.

gabaum10
  • 3,769
  • 3
  • 48
  • 89
  • 1
    do you want to be able to have it changed while the program is running? you currently are using a static value, but have a @property with a setter. – cobbal Dec 14 '10 at 16:25
  • 1
    Correction, I removed the property values and declarations. It seemed to do what I need, just need to verify this is the proper approach? – gabaum10 Dec 14 '10 at 16:28
  • I think you have it now. Basically the same thing I pointed out below. You want to avoid the duplicate declaration. – MystikSpiral Dec 14 '10 at 16:33
  • Yup solved that problem. Thanks for the responses. :) – gabaum10 Dec 14 '10 at 16:40

3 Answers3

11

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.

jxpx777
  • 3,632
  • 4
  • 27
  • 43
  • One last question, can I not use the variable I create in multiple classes? When I try to use it, it says "Duplicate variable". Is that a problem with this, or something else? – gabaum10 Dec 14 '10 at 17:02
  • 1
    Geoff, I should have mentioned that I usually put these kinds of constants in a AppNameConstants.h file and then add that to my .pch file for the project so it's available everywhere. That may or may not apply to your URL, but it's just another tip. – jxpx777 Dec 14 '10 at 18:14
0

I think this should work

#define DEBUG 1
#if DEBUG
   #define URL = @"dev.myserver.com";
#else
   #define URL = @"myserver.com";
#endif
Aaron Saunders
  • 33,180
  • 5
  • 60
  • 80
  • This is what I am doing now, will mark as answer as soon as I verify it is ok to have all in .h file... – gabaum10 Dec 14 '10 at 16:36
0

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
MystikSpiral
  • 5,018
  • 27
  • 22
  • Now I got it to work without the .m file. Is it standard practice to do it like this? Or is the way I have it ok. (see answer above) – gabaum10 Dec 14 '10 at 16:35
  • I normally do it this way (set the value in the init) but that does not mean it is right. I like this method because I can still have the variable as an accessible property. – MystikSpiral Dec 14 '10 at 16:37
  • Alright, mine just needs to be static and I don't need to access it as a property. Useful info though. Thanks for the reply – gabaum10 Dec 14 '10 at 16:39