是否可以禁止使用 NSLog ,以便在编译时使用它会出现错误? 理想情况下某种编译器标志具有不允许的方法名称?
由于
答案 0 :(得分:11)
如果您重新声明NSLog
(也许还有NSLogv
)
void NSLog(NSString *format, ...) UNAVAILABLE_ATTRIBUTE;
void NSLogv(NSString *format, va_list args) UNAVAILABLE_ATTRIBUTE;
在预编译的头文件中,您会收到一条错误消息:
main.m:199:3: error: 'NSLog' is unavailable NSLog(@"%@", s1); ^
您甚至可以提供自定义错误消息(可在Clang文档的Messages on deprecated and unavailable Attributes中找到):
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); ^
答案 1 :(得分:2)
在前缀标题中:
#define NSLog(x, ...) (__please_dont_use_NSLog__)
答案 2 :(得分:1)
试试这个!
#ifdef DEBUG
# define NSLog(...) NSLog(__VA_ARGS__)
#else
# define NSLog(...)
#endif
可以在此处找到解决方案:Enable and Disable NSLog in DEBUG mode
希望这有帮助!