Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining a globally accessible string in Objective-C

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

like image 506
teepusink Avatar asked Aug 16 '10 20:08

teepusink


2 Answers

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";
like image 192
Matt Williamson Avatar answered Oct 11 '22 01:10

Matt Williamson


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.

like image 37
Tim Rupe Avatar answered Oct 11 '22 01:10

Tim Rupe