Objective-c – NSColor with calibrated values works differently than regular color

cocoaobjective c

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.

Best Answer

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.

Related Topic