Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: String constants and how to do them



Kevin,

On Thursday, Mar 25, 2004, at 14:45 Europe/Prague, Kevin Ballard wrote:

I forget where I read this (perhaps it was Aaron Hillegass's book, but
I forget), but I read somewhere that in general it's a better idea to
use global strings set to a constant string rather than using #define,
as in

NSString *myPref = @"My Preference";

Rather, you should use

extern NSString * const OCSMyPrefName; // preferrably capitalized, being de-facto a constant, and prefixed, being a global

in a header, supplying the value in an appropriate implementation file.

instead of

#define myPref @"My Preference"

Is there a reason for this?

Well, it's cleaner and neater, it's a bit less error-prone (since the real value is compiled-in into the library and can't thus be spoilt by using of inappropriate header), it gives you the symbolic value in gdb, and it's a bit more memory-savvy (which is not too important, but it does not hurt too). Have I forgot anything of importance?

Oh yes, so far as the particular task allows that, you can compare directly ("if (x==OCSMyPrefName)") instead of being forced to use isEqualToString:.

Is this even true? What do you guys use?

Depends. In quick&dirty tools, #define's tend to be more straighforward, so I use them sometimes. In a code which would be used for long and re-used, I stick with the former (constant string) approach.
---
Ondra Hada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >String constants and how to do them (From: Kevin Ballard <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.