Quantcast
Channel: Global constants in Objective-C - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Global constants in Objective-C

$
0
0

I've 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 can I switch DEBUG to be True and False?


I've 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 doesn't 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 combine several global string variabile with other strings for create various urls.


UPDATE 1

Now Constants.m is:

#import "Constants.h"#ifdef DEBUG    #define DEF_BASE_URL "http://www.example.org/"#else    #define DEF_BASE_URL "http://localhost/"#endifNSString * 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 there is an error on the last line Parse error: expected ')'.Probably I can use CFSTR only with macros. I need to find a way for have all my global variables.


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images