5

I've just read all of objective-c global constants variables Q&A but I found them inappropriate to my problem.

I need a list of variable like this:

NSString *baseURL = @"http://example.org";
NSString *mediaURL = @"http://example.org/media/";
NSString *loginURL = @"http://example.org/login/";
NSString *postURL = @"http://example.org/post/";
etc.

Of course I can't use this code because it's a very bad approach and if I need to change the base url I must change all variables. Since I need these variables to be accessed from every class of the app I declared them as global with this approach:

// Constants.h
extern NSString *const baseURL;
extern NSString *const mediaURL;
extern NSString *const loginURL;
extern NSString *const postURL;


// Constants.m
NSString *const baseURL = @"http://example.org";
NSString *const mediaURL = [NSString stringWithFormat:"%@%@", baseURL, @"/media/"];
NSString *const loginURL = [NSString stringWithFormat:"%@%@", baseURL, @"/login/"];
NSString *const postURL = [NSString stringWithFormat:"%@%@", baseURL, @"/post/"];

BUT I can't do this because I get this error:

Initializer element is not a compile-time constant

This happens because objects works at runtime.

Now my question is, once and for all I hope, what is a nice and good way to handle this pretty usual scenario in network apps?

I think use a class (or a singleton class) for handle the constants variables is a bit overkill, and it's also too verbose use something like [MyClass globalVar] every time I need it.

Ideas about it?

Fred Collins
  • 4,664
  • 14
  • 58
  • 108

3 Answers3

4

I know it's old-fashioned but I'd just use preprocessor macros and let constant string concatenation handle it.

#define baseURL @"http://example.org"
#define mediaURL baseURL@"/media/"
Phillip Mills
  • 30,195
  • 4
  • 39
  • 55
  • Uhm, not bad sir. So in this case I need only one .h file, right? – Fred Collins Apr 07 '12 at 14:49
  • Yes. The defines are just pre-compile literal replacements. (Though, to be semi-standard, one might rename them as FC_BASE_URL and FC_MEDIA_URL or something of the sort.) – Phillip Mills Apr 07 '12 at 14:52
  • @PhillipMills have you any idea why xcode doesn't auto-suggest me for these variables names? – Fred Collins Apr 07 '12 at 15:40
  • Sorry...if you've imported the file that contains the defines, it should. I've definitely created things like that and had auto-completion work. – Phillip Mills Apr 07 '12 at 15:42
  • 1
    @FredCollins if you need it throughout the app you can import it in the Prefix.pch file.. then you don't need to import it in every file. – Ankit Srivastava Apr 07 '12 at 16:25
2
#define API_ROOT_URL @"https://www.example.org/api"
NSString *const OAuthUrl = API_ROOT_URL @"/oauthToken";

See also.

Community
  • 1
  • 1
sanmai
  • 23,569
  • 11
  • 54
  • 73
0

I don't have access to a MAC now ... can you just try this..

in .h

NSString *baseURLString;
extern NSString *const baseURL;

in .m

baseURLString= @"http://example.org";
NSString *const baseURL= [NSString stringWithFormat:"%@", baseURL];

I am not sure of this will work..

And also have you seen this answer at SO ... it has all the approaches I know of... Constants in Objective-C

Community
  • 1
  • 1
Ankit Srivastava
  • 11,975
  • 9
  • 57
  • 111