3

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.

4
  • 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. Commented Dec 14, 2010 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? Commented Dec 14, 2010 at 16:28
  • I think you have it now. Basically the same thing I pointed out below. You want to avoid the duplicate declaration. Commented Dec 14, 2010 at 16:33
  • Yup solved that problem. Thanks for the responses. :) Commented Dec 14, 2010 at 16:40

3 Answers 3

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.

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

2 Comments

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?
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.
1

I think this should work

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

1 Comment

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...
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

3 Comments

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)
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.
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

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.