tags:

views:

149

answers:

2

Hi,

What is the best way to define a globally accessible string?

I see that for integer it's usually like this #define easy 0

However, how can I emulate that for NSString?
I tried static NSString *BACKGROUND = @"bg.png";
While that work, it does give a warning saying the variable is never used. (I have all these in a .h file)

Doing NSString *const BACKGROUND = @"bg.png"; is even worse since it says duplicate variable when I import the file.

I see that #define BACKGROUND @"bg.png" seems to work too.

So I guess what is the difference between when to use #define, const & static

Thanks,
Tee

+6  A: 

This is the correct way to do it. Make some new blank .h file and .m. In your .h file:

extern NSString* const BACKGROUND;

In your .m file:

NSString* const BACKGROUND = @"bg.png";
Matt Williamson
It's actually `extern NSString *const`--otherwise, the contents of the string (i.e. `*BACKGROUND`) are marked const rather than the pointer.
Jonathan Grynspan
Gotcha, updated.
Matt Williamson
+2  A: 

You might want to consider using a property list to store your strings. This lets your code stay flexible for future updates, especially if you add support for localization.

Tim Rupe