Re: NSString* kMyKey = @"aKey" vs #define MYKEY @"aKey"
Re: NSString* kMyKey = @"aKey" vs #define MYKEY @"aKey"
- Subject: Re: NSString* kMyKey = @"aKey" vs #define MYKEY @"aKey"
- From: Michael Ash <email@hidden>
- Date: Sat, 4 Apr 2009 11:54:53 -0400
On Sat, Apr 4, 2009 at 6:15 AM, Uli Kusterer
<email@hidden> wrote:
> If you're inside a single, monolithic application, yes. However, #define is
> eliminated by the preprocessor, whereas kMyKey would still be exported to
> the linker, e.g. in the case of a framework. Which means you can make 100%
> sure that this one key string is shared by anyone in the current address
> space ("linked into the application") who is using your framework. That in
> turn means you can compare the actual address of the string, without having
> to look at its contents.
Danger Will Robinson! Given the semantics of an NSString, you should
never rely on external code giving you the same pointer that you gave
it. In other words, never rely on comparing NSStrings by pointer
equality (at least never do it if the pointer ever passed through code
that isn't yours). It would be perfectly legitimate to copy the string
and then still use it as though it were the original.
It's perfectly reasonable to use pointer equality *first* before you
perform content equality, as an optimization, and no doubt
-isEqualToString: does this. And it would be perfectly reasonable to
use the global variable to ensure that this optimization can be
performed under as many circumstances as possible. But you shouldn't
rely only on pointer equality even if you're using the global
variable.
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden