#define macros and xcode warning and lack of code hinting
I wrote up some small macros to help in setting application-wide font styles, colors and gradients. The issue is that xcode is throwing warnings whenever I use the color or font defined in the macro. Is there any way to get xcode to validate the macro, and hopefully get code hinting as well?
#define HEX_COLOR(colorname, hexcolor) \
\
@implementation UIColor(Style##colorname) \
\
+ (UIColor*) colorname \
{ \
static UIColor * staticColor = nil; \
if(! staticColor) { \
float red = ((hexcolor & 0xFF0000) >> 16)/255.0; \
float green = ((hexcolor & 0xFF00) >> 8)/255.0; \
float blue = (hexcolor & 0xFF)/255.0; \
float alpha = (hexcolor >> 24)/255.0; \
\
staticColor = [[UIColor colorWithRed:red \
green:green \
blue:blue \
alpha:alpha] retain]; \
} \
return staticColor; \
} \
@end
In my app delegate i set the application-wide fonts and colors like this:
HEX_COLOR(specialGreenColor, 0x66BAD455);
.....
This line throws the warning that the property might not exist.
[[UIColor specialGreenColor] set];
Also I do not want to lessen the error reporting in xcode as not seeing warning is a backwards step. I just would l开发者_运维问答ike to find a way to regiester the marco with xcode.
If you want to define constants, you should use #define
like so:
#define COLOUR_NAME 0x66BAD455
Then, the preprocessor will go through your file and replace all instances of COLOUR_NAME
verbatim with 0x66BAD455
.
There are arguably better ways to define application-wide constants though.
Edit: there's also this nice post which seems to provide a better implementation of what you're going for. You can define the macro and then define your colour constants using the question linked above.
Code hinting for the marco works in xCode 4.2
精彩评论