• 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: defining NSColor constants
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: defining NSColor constants


  • Subject: Re: defining NSColor constants
  • From: Uli Kusterer <email@hidden>
  • Date: Tue, 11 Aug 2009 07:53:20 +0200

On Aug 6, 2009, at 8:58 AM, Arie Pieter Cammeraat wrote:
NSColor * const kNiceBlueColor = [NSColor colorWithRed: 20 green: 20 blue: 240 alpha:1];

Don't assign autoreleased objects to global variables/global constants, please. The object will get autoreleased and the constant will point to the address of an object that is long gone. Stuff like that leads to bugs that are very hard to track down. (unless you're running with the garbage collector, then the code above is mostly OK).


The best appproach suggested so far is a category on NSColor where you implement:

+(NSColor*) mySoylentGreen
{
static NSColor* sMySoylentGreen = nil; // Static var -- global that's invisible outside this method. Initialization only happens the first time.
if( sMySoylentGreen == nil )
sMySoylentGreen = [[NSColor alloc] initWithCalibratedRed: 0 green: 1.0 blue: 0];
return sMySoylentGreen;
}


Why do this?

1) It creates the color lazily, as needed, the first time someone uses it. If you create all your colors right at startup, this makes your app slower to start up. This way, the long startup wait is actually turned into many split-second waits distributed throughout the runtime of your program.

2) It's efficient, because the object in the sMzSoylentGreen variable is created exactly once and then re-used. If you used NSColor colorWithCalibratedRed:green:blue with the actual numbers in a loop, you would create a new object each time.

3) It is an "intentional leak". Usually, you would have to release an object created using an alloc/init call. But since it is in a global variable (sMySoylentGreen), you can always access it, and since you'll probably keep using it for the rest of your app's lifetime, you can let the OS recycle it along with all your app's memory when your application shuts down.

Hope that helps,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de





_______________________________________________

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: 
 >defining NSColor constants (From: Arie Pieter Cammeraat <email@hidden>)

  • Prev by Date: Re: Trouble with event taps...
  • Next by Date: Re: Disabled controls don't update their visual appearance
  • Previous by thread: Re: defining NSColor constants
  • Next by thread: resolving addresses for the system framework
  • Index(es):
    • Date
    • Thread