禁止使用 NSLog [英] Disallow NSLog to be used

查看:76
本文介绍了禁止使用 NSLog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以禁止使用 NSLog,以便在编译时使用它会出现错误?理想情况下,某种带有禁止方法名称的编译器标志?

谢谢

解决方案

如果你重新声明 NSLog(也许还有 NSLogv)为

void NSLog(NSString *format, ...) UNAVAILABLE_ATTRIBUTE;void NSLogv(NSString *format, va_​​list args) UNAVAILABLE_ATTRIBUTE;

在您的预编译头文件中,您会收到一条很好的错误消息:

<前>main.m:199:3: 错误:'NSLog' 不可用NSLog(@"%@", s1);^

<小时>

您甚至可以提供自定义错误消息(可在 消息中找到Clang 文档中已弃用和不可用的属性:

void NSLog(NSString *format, ...) __attribute__((unavailable("你不应该这样做!")));

<前>main.m:202:3: 错误:'NSLog' 不可用:你不应该这样做!NSLog(@"%@", s1);^

Is it possible to disallow the use of NSLog, so that it will come up as an error if used at compile time? Ideally some sort of compiler flag with the name of the method that is disallowed?

Thanks

解决方案

If you re-declare NSLog (and perhaps also NSLogv) as

void NSLog(NSString *format, ...) UNAVAILABLE_ATTRIBUTE;
void NSLogv(NSString *format, va_list args) UNAVAILABLE_ATTRIBUTE;

in your precompiled header file, you get a nice error message:

main.m:199:3: error: 'NSLog' is unavailable
                NSLog(@"%@", s1);
                ^


You can even provide a custom error message (found in Messages on deprecated and unavailable Attributes of the Clang documentation):

void NSLog(NSString *format, ...) __attribute__((unavailable("You should not do this!")));


main.m:202:3: error: 'NSLog' is unavailable: You should not do this!
                NSLog(@"%@", s1);
                ^

这篇关于禁止使用 NSLog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆