Global constants in Objective-C

I have a file called Constants.h :

extern NSString * const BASE_URL; 

and Constants .m :

 #ifdef DEBUG NSString * const BASE_URL = @"http://www.example.org "; #else NSString * const BASE_URL = @"http://localhost"; #endif 

First question: how to switch DEBUG to True and False ?


I have a view controller file MyViewController.m :

 #import "MyViewController.h" #import "Constants.h" // this doesn't works. see above for the error. static NSString * const ANOTHER_URL = [NSString stringWithFormat:@"%@%@", BASE_URL, @"/path/"]; @implementation HomeViewController [...] 

The code does not work and returns me this error:

 error: Semantic Issue: Initializer element is not a compile-time constant 

How can i fix this?

I need to chain several global variables with other strings to create different URLs.


UPDATE 1

Now Constants.m :

 #import "Constants.h" #ifdef DEBUG #define DEF_BASE_URL "http://www.example.org/" #else #define DEF_BASE_URL "http://localhost/" #endif NSString * const BASE_URL = (NSString*)CFSTR(DEF_BASE_URL); NSString * const API_URL = (NSString*)CFSTR(DEF_BASE_URL "api/"); NSString * const API_SETTINGS_URL = (NSString*)CFSTR(API_URL "settings/"); 

But in the last line there is an error. Analysis error: expected ')' . I can probably use CFSTR with macros only. I need to find a way to have all my global variables.

+8
objective-c global-variables iphone ios5
source share
3 answers

Solution A:

Personally, I would just use the function for ANOTHER_URL .

Solution B:

If you really want a constant: you should use the cstring concatenation rules via #define and then pass this via CFSTR() :

 // defs.h extern NSString * const BASE_URL; extern NSString * const ANOTHER_URL; // defs.m #ifdef DEBUG #define DEF_BASE_URL "http://www.example.org" #else #define DEF_BASE_URL "http://localhost" #endif NSString * const BASE_URL = (NSString*)CFSTR(DEF_BASE_URL); NSString * const ANOTHER_URL = (NSString*)CFSTR(DEF_BASE_URL "/path/"); 

Solution C:

If you want to create only one via initialization, you can also execute a local static function / method in C ++ / ObjC ++ translations (then, when necessary, use the visibility of C or ObjC):

 NSString * URL() { static NSString * const ANOTHER_URL = [NSString stringWithFormat:@"%@%@", BASE_URL, @"/path/"]; return ANOTHER_URL; } 
+6
source share

First, you can tell Xcode to install some preprocessor macros for debugging collections. Use the preprocessor macro to build.

For your second question, you cannot call the objective-C method to fill in the constant, because this material is not available at compile time. Your best option is to define a global variable and then give it a value in the classโ€™s โ€œinitializeโ€ method.

 static NSString * ANOTHER_URL; + initialize { ANOTHER_URL = [NSString stringWithFormat:@"%@%@", BASE_URL, @"/path/"]; } 

initialize is called before the first instance of the class is created, so it is safe. You will have to give up the const keyword, but I'm sure you can trust yourself !;)

+1
source share

#define DEBUG 1 or 0 to disable.

The error is due to the fact that you are calling the NSString method, and its compilation constant. In other words, you tell the compiler that it simply cannot handle it. You will need to initialize such a dynamic link at startup, the compiler will not be able to do this for you.

0
source share

All Articles