• 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: Global variable for whole application.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Global variable for whole application.


  • Subject: Re: Global variable for whole application.
  • From: Uli Kusterer <email@hidden>
  • Date: Thu, 19 Apr 2007 11:04:02 +0200

Am 19.04.2007 um 06:41 schrieb Tony S. Wu:
my approach is to create a class that handles global variables.

Me too, I usually have some singleton that contains the code that accesses the global, so I just make the "global" an instance variable of that singleton.


To store the singletons themselves, I generally use file-static globals in the implementation file, or static variables in a function, and write an accessor function a la [NSUserDefaults sharedUserDefaults]. That way I avoid name collisions with other classes, and prevent spaghetti code because someone directly changes my global instead of going through my class.

For notification names and other strings that need to be pointer- comparable, I use regular globals in implementation files (const, if using a C version that lets me do that), plus extern declarations in the class header. That runs the risk of name collisions, but since they need to be externally accessible, that's a given anyway, and generally the linker will catch the duplicate declaration.

For integer constants, bit flags, four-char-codes and character constants, I usually use enums. enums have the advantage that they can be named and used as a type and can thus be used to ensure nobody passes the wrong constant to my function. Not to mention they serve as self-documenting code because nobody has to look up in the documentation what valid values are, and the compiler even catches wrong uses. And even if I have to allow other values, I can at least refer to the enum by name to tell people where to find the special values.

For floating point constants, C-string constants and almost anything else, I use #defines if there's no better alternative.

Cheers,
-- M. Uli Kusterer
http://www.zathras.de



_______________________________________________

Cocoa-dev mailing list (email@hidden)

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


  • Follow-Ups:
    • Re: Global variable for whole application.
      • From: Brian Willoughby <email@hidden>
References: 
 >Global variable for whole application. (From: "Jay" <email@hidden>)
 >Re: Global variable for whole application. (From: Filipe Varela <email@hidden>)
 >Re: Global variable for whole application. (From: "Shawn Erickson" <email@hidden>)
 >Re: Global variable for whole application. (From: Filipe Varela <email@hidden>)
 >Re: Global variable for whole application. (From: "Shawn Erickson" <email@hidden>)
 >Re: Global variable for whole application. (From: "Tony S. Wu" <email@hidden>)

  • Prev by Date: Re: Migration Path for NSArchiver data?
  • Next by Date: [Cocoa] How convert ":Developer:Private:jam" into a path POSIX-style
  • Previous by thread: Re: Global variable for whole application.
  • Next by thread: Re: Global variable for whole application.
  • Index(es):
    • Date
    • Thread