• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSString* kMyKey = @"aKey" vs #define MYKEY @"aKey"
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSString* kMyKey = @"aKey" vs #define MYKEY @"aKey"


  • Subject: Re: NSString* kMyKey = @"aKey" vs #define MYKEY @"aKey"
  • From: Greg Parker <email@hidden>
  • Date: Mon, 6 Apr 2009 10:33:14 -0700

On Apr 3, 2009, at 3:44 PM, Quincey Morris wrote:
On Apr 3, 2009, at 15:15, Nate Weaver wrote:
IIRC they're optimized to point to the same memory location (I wasn't sure, so I tested and confirmed).

I usually do:

NSString * const kConstantNameHere = @"foo";

That's what I've seen in Apple headers (with an extern at the beginning and no assignment in said headers, of course).

Except that the reason for *that* is to have the names of the strings in the public API but to keep the content of the strings out of it, which is presumably not a consideration for the OP.

The other reason for it is to guarantee that kConstantNameHere has the same address everywhere. If you use a #define, you'll get a different string object (with its own address) in each framework or executable. The linker's string optimization only works inside a single linkage unit.



-- Greg Parker email@hidden Runtime Wrangler


_______________________________________________

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


References: 
 >NSString* kMyKey = @"aKey" vs #define MYKEY @"aKey" (From: Memo Akten <email@hidden>)
 >Re: NSString* kMyKey = @"aKey" vs #define MYKEY @"aKey" (From: Nate Weaver <email@hidden>)
 >Re: NSString* kMyKey = @"aKey" vs #define MYKEY @"aKey" (From: Quincey Morris <email@hidden>)

  • Prev by Date: Re: Non-pageable app
  • Next by Date: NSWindow from code
  • Previous by thread: Re: NSString* kMyKey = @"aKey" vs #define MYKEY @"aKey"
  • Next by thread: Re: NSString* kMyKey = @"aKey" vs #define MYKEY @"aKey"
  • Index(es):
    • Date
    • Thread