Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare and use NSString global constants [duplicate]

Possible Duplicate:
Constants in Objective C

I store some app settings in NSUserDefaults. NSStrings are used as keys. The problem is I need to access these settings throughout the app using those NSString keys. There is a chance that I mistype such string key when accessing in some part of the app.

Throughout the app, I have such statements

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"ReminderSwitch"];

BOOL shouldRemind = [[NSUserDefaults standardUserDefaults] boolForKey:@"ReminderSwitch"];

How and where can I declare a global NSString constant which I can access throughout the app. I will then be able to use that constant without worrying about mistyping those string keys.

like image 963
Anand Avatar asked Aug 13 '12 10:08

Anand


4 Answers

First, you should go for a real extern C symbol -- not a macro. this is done like so:

SomeFile.h

extern NSString *const MONConstantString;

SomeFile.m

NSString *const MONConstantString = @"MONConstantString";

note that if you use a mix of ObjC and ObjC++, you will need to specify extern "C" for C++ TUs -- that's why you will see a #defined export which varies by language.


Then, you will want to put the constant near the interfaces it relates to. Taking your example as a lead, you might want a set of interfaces or declarations for your app's preferences. In that case, you might add the declaration to MONAppsPreferences header:

MONAppsPreferences.h

extern NSString *const MONApps_Pref_ReminderSwitch;

MONAppsPreferences.m

NSString *const MONApps_Pref_ReminderSwitch = @"MONApps_Pref_ReminderSwitch";

In use:

#import "MONAppsPreferences.h"
...
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:MONApps_Pref_ReminderSwitch];
like image 174
justin Avatar answered Oct 18 '22 07:10

justin


Your thought is right I think. For example, I made Const.h/m file like below:

Const.h

extern NSString *const UserIdPrefKey;
extern NSString *const PasswordPrefKey;
extern NSString *const HomepagePrefKey;

Const.m

#import "AEConst.h"

NSString *const UserIdPrefKey = @"UserIdPrefKey";
NSString *const PasswordPrefKey = @"PasswordPrefKey";
NSString *const HomepagePrefKey = @"UrlHomepagePrefKey";

Only Const.h must be imported.

When you write code, Xcode supports writing the key name so that you can avoid miss-typing.

like image 26
Feel Physics Avatar answered Oct 18 '22 08:10

Feel Physics


What you seem to be looking for is just a way to define string constants in your app.

See this question and this answer to it, which I've quoted below:

You should create a header file like

// Constants.h 
FOUNDATION_EXPORT NSString *const MyFirstConstant;
FOUNDATION_EXPORT NSString *const MySecondConstant;
//etc.

You can include this file in each file that uses the constants or in the pre-compiled header > for the project.

You define these constants in a .m file like

// Constants.m 
NSString *const MyFirstConstant = @"FirstConstant";
NSString *const MySecondConstant = @"SecondConstant"; 

Constants.m should be added to your application/framework's target so that it is linked in to the final product.

The advantage of using string constants instead of #define'd constants is that you can test for equality using pointer comparison (stringInstance == MyFirstConstant) which is much faster than string comparison ([stringInstance isEqualToString:MyFirstConstant]) (and easier to read, IMO).

With thanks to Barry Wark :)

like image 37
Erik S Avatar answered Oct 18 '22 09:10

Erik S


The easiest way to do this is make simple .h file, like Utils.h and write there following code:

#define kUserDefaults @"ReminderSwitch"

like image 37
Artem Kalachev Avatar answered Oct 18 '22 07:10

Artem Kalachev