我想在调试时启用 NSLog,否则禁用它。一个很简单的事情是:
#ifdef DEBUG
NSLog(@"My log");
#endif
但是所有这些 #ifdef
和 #endif
都很无聊...... :( 所以我尝试了其他东西:(.pch 是放置它的好地方)
#ifdef DEBUG
# define NSLog(text) NSLog(text);
#else
# define NSLog(text)
#endif
这项工作非常好(不是递归的)。但问题是 NSLog 有无限的参数。
void NSLog(NSString *format, ...)
如何解决这个问题以在预处理器模式下工作?
-- 编辑--
这段代码让你的 NSLog 变得更好:
#ifdef DEBUG
#define NSLog(FORMAT, ...) fprintf(stderr,"%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
#define NSLog(...)
#endif
最佳答案
这应该可以解决问题:
#ifdef DEBUG
# define NSLog(...) NSLog(__VA_ARGS__)
#else
# define NSLog(...) (void)0
#endif
关于objective-c - 在 DEBUG 模式下启用和禁用 NSLog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6552197/