Disallow NSLog to be used

孤人 提交于 2019-12-22 17:05:26

问题


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


回答1:


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);
                ^



回答2:


In your prefix header:

#define NSLog(x, ...) (__please_dont_use_NSLog__)



回答3:


Try this!

 #ifdef DEBUG
 #   define NSLog(...) NSLog(__VA_ARGS__)
 #else 
 #   define NSLog(...)
 #endif

The solution can be found here: Enable and Disable NSLog in DEBUG mode

Hope this helped!



来源:https://stackoverflow.com/questions/16069402/disallow-nslog-to-be-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!