Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you use constants when working with NSDictionary? [closed]

I understand using constants for your names in a NSDictionary to prevent typos (myName will auto complete vs @"myName" won't).

i'm working with a medium size dictionaries right now and a couple of times, i've misstyped key names and had to spend some time tracking down where i miss spelled a word.

i'm wondering, do you consider it worth while to set up a constants naming scheme?

like image 945
Padin215 Avatar asked Feb 10 '12 17:02

Padin215


1 Answers

Yes, but I'd advise defining proper string constants rather than #defines for your strings because string constants are type-safe and every reference will use the same actual string object, which improves the performance of using isEqualToString: to compare them.

To define a private string constant you can put this in your .m file:

static NSString *const MyConstant = @"MyConstant";

To make it public, you can either put this in your .h file instead, or you can split the definition by putting this in your .h:

extern NSString *const MyConstant;

And this in your .m file:

NSString *const MyConstant = @"MyConstant";
like image 83
Nick Lockwood Avatar answered Sep 20 '22 06:09

Nick Lockwood