• 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: Creating Dictionary Keys
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Creating Dictionary Keys


  • Subject: Re: Creating Dictionary Keys
  • From: Bob Ippolito <email@hidden>
  • Date: Mon, 31 May 2004 11:05:11 -0400

On May 31, 2004, at 10:13 AM, Phill Kelley wrote:

> I usually declare things like dictionary keys using the C
> preprocessor's
> #define mechanism but I've noticed that other people seem to prefer
> global
> variables (eg "extern NSString" in a .h file with a constant
> assignment in
> the .m file).
>
> To me, the #define mechanism seems simpler, both to implement and
> maintain,
> because it keeps everything in one place, albeit at the expense of
> having
> multiple copies of the string floating around in memory if the string
> is
> referenced in more than one class.
>
> Is the reason why so many Cocoa programmers prefer the global-variable
> approach simply to guarantee that there is only a single copy of each
> string in memory, or is there more to it than that?

Using an extern for the string gives you several advantages in the case
of a library (or framework), including but not limited to:
- You can change it without recompiling programs that link against the
library, they will still work because they only need the symbol name
- You don't need the headers to know what the string is, you only need
to know the symbol name (good for language bridging and reverse
engineering stuff)
- Not only is there a single copy of each string in memory, but there's
a single copy of each string in memory for the whole system.. it should
end up in the read-only segment of the dynamic library and will be
shared between processes.
- It makes the programs that use the string it slightly smaller because
they don't need to compile in the constant, only a reference to the
constant from some library.
- It doesn't use the preprocessor, which makes things somewhat easier
to maintain and debug.

-bob

[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
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.


  • Prev by Date: beginSheetForDirectory problem
  • Next by Date: Re: NSSearchField Menu Enabling Troubles...
  • Previous by thread: Re: Creating Dictionary Keys
  • Next by thread: beginSheetForDirectory problem
  • Index(es):
    • Date
    • Thread