Challenge with NSColor Constant



Figured out the challenge, sort of. I figured that during this challenge it would be a good time to set constants for the background color and the checkbox default values. My plan is to adjust the +initializer method with these constants. I figured this is better programming practice (if this isn’t please let me know), so I tried this line:

extern NSColor * const BNRDefaultTableBackgroundColorPreference;

NSColor * const BNRDefaultTableBackgroundColorPreference = [NSColor lightGrayColor];

No problems with Xcode in the header, but in the implementation I get “Initializer element is not a compile-time constant.”

I’m stumped. Any thoughts?


As Xcode is telling you, and as you may have gleaned form the error, you can’t create a const to a value that’s not known at compile time. Because you’d have to call a method ([NSColor lightGrayColor]) to get the pointer value, this breaks that rule.

You could go a few ways with it, including making a #define macro, but I think my favorite approach would be to create a category on NSColor:

[code]@interface NSColor (BNRDefaults)

  • (NSColor *)bnrDefaultTableBackgroundColor;

Categories are covered in Chapter 22.



I skipped ahead and browsed Ch. 22. Makes sense.

BTW, I appreciate the help. The support you give is fabulous, but with it being mainly you writing back, I’m sure it gets tiresome.

Thanks again.