【发布时间】:2011-09-26 23:43:33
【问题描述】:
我想在调试时启用 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
【问题讨论】:
-
+1 表示好问题。关于此主题的完整可重用组件mobile.tutsplus.com/tutorials/iphone/…
-
我按照链接中的指南进行操作。但我收到以下编译错误:架构 x86_64 的未定义符号:ld:未找到架构 x86_64 的符号 clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) 请帮助@MahbuburRAaman
-
@Gon, Error Undefined symbols for architecture x86_64: ld: symbol(s) not found for architecture x86_64 出现有几个原因。例如,对于缺少库或其他情况,请查看以下 SO 资源 stackoverflow.com/questions/18408531/…、stackoverflow.com/questions/11996227/…、stackoverflow.com/questions/6231368/…。
-
@MahbuburRAaman 感谢您的回复。我发现原因是我从一个目标 c++ 文件中调用了这个自定义的 NSLog 方法。
-
请问,我该把这段代码放在哪里?
标签: objective-c cocoa nslog