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.
objective-c global-variables iphone ios5
Fred collins
source share