views:

358

answers:

3

I'm using a method in my view to set a color, and in awakeFromNib I pass it a color using [NSColor colorWithCalibratedRed: green: blue: alpha:]

The application kept crashing, with the error with "[NSCFNumber set] unrecognized selector." After inserting a breakpoint, I found it was defining my variable as an "NSCalibratedRGBColor." The application worked when I defined the color with one of the convenience methods (blueColor, whiteColor, etc.). I thought those were just a shortcut for setting RGB values. I have no idea why I haven't run into this problem before, I've used colors like this a lot. Why does it handle this differently, and can I make it interpret it as a regular color?

EDIT: The code is: [self setLineColor:[NSColor colorWithCalibratedRed:green:blue:alpha]; in my awakeFromNib. I've also discovered that it is a non-1 alpha value that causes the color to be defined "NSCalibratedRGBColor." Alpha values of 1, like the convenience methods, cause the color to be defined "NSCachedRGBColor" in the debug, which works completely fine.

A: 

What was the line of code that was causing a problem?

NSCalibratedRGBColor does work differently than a non-calibrated color, in that when you "set" it, the color will be calibrated to the context's colorspace, rather than using the raw uncalibrated color.

However, I've never seen this error on this particular usage of NSColor methods, and without further information, I'm left scratching my balding head.

NilObject
The line is:[self setLineColor:[NSColor colorWithCalibratedRed:green:blue:alpha];in my awakeFromNib.I've also discovered that it is a non-1 alpha value that causes the color to be defined "NSCalibratedRGBColor." (let me continue the comment)...
Alpha values of 1, like the convenience methods, cause the color to be defined "NSCachedRGBColor" in the debug, which works completely fine. Any suggestions?
Actually, Peter Hosey is right. The Cached RGB colors already have a reference retained (hence the "cached" name), and thus your setLineColor method is probably not retaining the color that it's saving off.
NilObject
+2  A: 

The application kept crashing, with the error with "[NSCFNumber set] unrecognized selector."

That means that you over-released the color, and then another object (in this case, an NSNumber) got allocated to the same pointer. Then you sent the set message to the object that you thought was your color, but it was actually now an NSNumber object. Result: That error. It had nothing to do with your use of a calibrated vs. uncalibrated color space.

All the colors support the same NSColor interface. The NS[snip]Color classes you're seeing are private subclasses of NSColor; they all support all of NSColor's methods. As far as you're concerned, they are all just NSColors.

Peter Hosey
Thanks alot, as someone else suggested, I just turned on garbage collection. It works now. :)
A: 
fish