Global constants in Objective-C?

Solution A: Personally, I would just use a function for ANOTHER_URL Solution B: If you really want a constant: You should be able to use cstring concatenation rules via define then pipe that through 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 just one via initialization, you can also accomplish a function/method local static in C++/ObjC++ translations (then use C or ObjC visibility, where needed): NSString * URL() { static NSString * const ANOTHER_URL = NSString stringWithFormat:@"%@%@", BASE_URL, @"/path/"; return ANOTHER_URL; }.

Solution A: Personally, I would just use a function for ANOTHER_URL. Solution B: If you really want a constant: You should be able to use cstring concatenation rules via #define, then pipe that through 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 just one via initialization, you can also accomplish a function/method local static in C++/ObjC++ translations (then use C or ObjC visibility, where needed): NSString * URL() { static NSString * const ANOTHER_URL = NSString stringWithFormat:@"%@%@", BASE_URL, @"/path/"; return ANOTHER_URL; }.

Thanks dude. But I've a problem. If I place NSString * const ANOTHER_URL = (NSString*)CFSTR(DEF_BASE_URL "/path/"); in MyViewController.

M before the @implementation it returns me an error: Parse error: Expected ). I'm sure the code is written okay. Probably I can't use it in viewcontrollers?

Why? In Constants. M works well without errors.

– Fred Collins 1 hour ago @Fred copy/paste error - there was a trailing semicolon following DEF_BASE_URL's value. – Justin 1 hour ago checkout my update, thanks. – Fred Collins 1 hour ago @Fred the trick is valid for c strings, but not CF/NSStrings.So you can build/compose them using c strings, then send the result through CFSTR - but it cannot take a form like: cstring_literal+NSString to produce a CF/NSString literal.

So just build them using c strings. – Justin 1 hour ago How? And in your opinion, is the best way to do this?

If you have understood well what I'm trying to achieve I've a BASE_URL and for each view controllers I've some urls composed by BASE_URL and other strings. – Fred Collins 57 mins ago.

Firstly, you can tell Xcode to set some preprocessor macros for debug builds. Use the build option 'preprocessor macros'. For your second question, you can't call an objective-C method to fill a constant, because that stuff isn't available at compile-time.

Your best option is to define the global variable, then give it a value in the class 'initialize' method. Static NSString * ANOTHER_URL; + initialize { ANOTHER_URL = NSString stringWithFormat:@"%@%@", BASE_URL, @"/path/"; } initialize is called before your first instance of the class is created, so it's safe. You'll have to drop the const keyword, but I'm sure you can trust yourself!

;).

Define DEBUG 1 or 0 to disable. The error is because you are calling a method of NSString while its a compile constant. In other words you are telling the compiler something it simply can't handle.

You will need to initialize such a dynamic link on launch, the compiler can't do this for you.

– Fred Collins 1 hour ago wow lol # apparently makes a header in an SO post.. sorry for that. Anyways you can always do somehting like that in + (void)initialize which is called before your class is created. – Antwan van Houdt 1 hour ago.

This is a very tricky task to handle constants in Objective-C. The most suggested approach is to define global, public or private constants. This will work only if you don’t expect to put the same constant inside a loop.

You can include this file in each file that uses the constants or in the pre-compiled header for the project. M should be added to your application/framework’s target to link it to product. It is suggested that the advantage of using string constants instead of #define‘d constants is that you can test for equality using pointer comparison (stringInstance == strConstant) which is much faster than string comparison (stringInstance isEqualToString:strConstant).

If you need a non global constant, you should use static keyword. Static constant is not visible outside the file. Another quick dirty way of declaring global constants is to put constant declaration into pch file instead of .

To declare integer instead of string in Objective-C, you will use NSInteger. This is Apple preferred way of declaring Objective-C integer. However if you are going to use this method to declare NSInteger and use it inside a loop, then you are in trouble.

If you need to use a constant inside a IF statement and iterate the constant inside a class instance or method, you would have to use static keyword. You can add static int outside @implement.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions