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
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";
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With