#define 宏和 xcode 警告以及缺少代码提示
我编写了一些小宏来帮助设置应用程序范围的字体样式、颜色和渐变。问题是,每当我使用宏中定义的颜色或字体时,xcode 都会发出警告。有什么方法可以让 xcode 验证宏,并希望获得代码提示吗?
#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
在我的应用程序委托中,我设置了应用程序范围的字体和颜色,如下所示:
HEX_COLOR(specialGreenColor, 0x66BAD455);
.....
此行引发该属性可能不存在的警告。
[[UIColor specialGreenColor] set];
另外,我不想减少 xcode 中的错误报告,因为看不到警告是一种倒退。我只是想找到一种用 xcode 注册 marco 的方法。
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 like to find a way to regiester the marco with xcode.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想定义常量,您应该像这样使用
#define
:然后,预处理器将遍历您的文件并逐字替换
COLOUR_NAME
的所有实例 em> 与0x66BAD455
。不过,可以说有更好的方法来定义应用程序范围的常量。
编辑:还有这篇不错的帖子,它似乎提供了您的更好的实现我们要去的。您可以定义宏,然后使用上面链接的问题定义颜色常量。
If you want to define constants, you should use
#define
like so:Then, the preprocessor will go through your file and replace all instances of
COLOUR_NAME
verbatim with0x66BAD455
.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.
marco 的代码提示适用于 xCode 4.2
Code hinting for the marco works in xCode 4.2