#define 宏和 xcode 警告以及缺少代码提示

发布于 2024-12-05 15:24:10 字数 1108 浏览 0 评论 0原文

我编写了一些小宏来帮助设置应用程序范围的字体样式、颜色和渐变。问题是,每当我使用宏中定义的颜色或字体时,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半岛未凉 2024-12-12 15:24:10

如果您想定义常量,您应该像这样使用#define

#define COLOUR_NAME 0x66BAD455

然后,预处理器将遍历您的文件并逐字替换COLOUR_NAME的所有实例 em> 与 0x66BAD455

不过,可以说有更好的方法来定义应用程序范围的常量。

编辑:还有这篇不错的帖子,它似乎提供了您的更好的实现我们要去的。您可以定义宏,然后使用上面链接的问题定义颜色常量。

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.

自演自醉 2024-12-12 15:24:10

marco 的代码提示适用于 xCode 4.2

Code hinting for the marco works in xCode 4.2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文