• 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: Alastair Houghton <email@hidden>
  • Date: Thu, 6 Aug 2009 09:32:04 +0100

On 6 Aug 2009, at 07:58, Arie Pieter Cammeraat wrote:

I would like a more obj-c style, like
	globals.h:
		extern NSColor * const kNiceBlueColor

	globals.m:
	#import globals.h

NSColor * const kNiceBlueColor = [NSColor colorWithRed: 20 green: 20 blue: 240 alpha:1];

I found some examples of this kind for defining a NSString using the @"foo"-syntax. With NSColor, it doesn't seem to work. I get the error "initializer element is not constant". Anyone any thoughts on this?


You can do something like your example above, but because you aren't allowed to send messages (or to call functions) from initialisers of global variables in C, you'd need to initialise them in some other way (for instance from an appropriate +initialize method).

Other alternatives (possibly safer, since you can't rely on the order in which +initialize methods are called) include:

-  Adding your new colours as new methods on a category of NSColor.

- Adding your new colours as new methods on some global object in your application.

- Adding your new colours as new methods on some other appropriate object in your app.

In all of these alternative cases, you'd probably actually end up with a static NSColor * and implement something like this:

  + (NSColor *)myNiceBlueColour
  {
    static NSColor *myNiceBlueColour;

if (!myNiceBlueColour)
myNiceBlueColour = [[NSColor alloc] initWithRed:0.1 green:0.1 blue:0.8 alpha:1.0];


    return myNiceBlueColour;
  }

(Remember, by the way, that you need to use floating point values between 0 and 1, rather than the integers you show in your example above.)

Kind regards,

Alastair.

--
http://alastairs-place.net



_______________________________________________

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


  • Follow-Ups:
    • Re: defining NSColor constants
      • From: Graham Cox <email@hidden>
References: 
 >defining NSColor constants (From: Arie Pieter Cammeraat <email@hidden>)

  • Prev by Date: Re: [iPhone] networking
  • Next by Date: Re: [iPhone] networking
  • Previous by thread: defining NSColor constants
  • Next by thread: Re: defining NSColor constants
  • Index(es):
    • Date
    • Thread