xcode 如何禁用 NSLog?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6732333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I disable NSLog?
提问by CodeGuy
I'd like to do the following in Xcode:
我想在 Xcode 中执行以下操作:
Find all NSLog commands without comments, and replace it with //NSLog...
找到所有没有注释的 NSLog 命令,并用 //NSLog... 替换它
In other words, I want to comment all NSLog calls. Is this possible? Is there an easy way to do this?
换句话说,我想评论所有 NSLog 调用。这可能吗?是否有捷径可寻?
采纳答案by Mihai Fratu
Update:
更新:
The answer bellow is actually much better. See here.
下面的答案实际上要好得多。看到这里。
Initial answer:
初步回答:
There is a little hack that you could do. Search for all NSLogand replace them with //NSLogand than do another search for ////NSLogand replace them with //NSLog.
你可以做一些小技巧。搜索所有NSLog并用 替换它们,//NSLog然后再搜索////NSLog并用 替换它们//NSLog。
回答by Saqib Saud
wait there is far more simple method. Just add the following lines when you dont need NSLOG to your constants file
等待有更简单的方法。当您不需要 NSLOG 到您的常量文件时,只需添加以下几行
#define NSLog //
and comment it when you need it.
并在需要时发表评论。
EDIT: In latest Xcode, you get error on above statement. So I figured out the best way is
编辑:在最新的 Xcode 中,您在上述语句中遇到错误。所以我想出了最好的方法是
#define NSLog(...)
回答by Prateek Prem
#define NSLog if(1) NSLog
if you dont want log set 1 as 0.
如果您不想将日志设置为 1 为 0。
回答by Marcus Schwab
I have to do a separate answer because I cant comment yet, but one thing you really need to be careful about when you do a find and replace is something like this:
我必须做一个单独的答案,因为我还不能发表评论,但是当你进行查找和替换时,你真的需要小心的一件事是这样的:
if(BOOL)
NSLog(@"blah blah");
[self doSomething];
If you comment out the nslog, the conditional gets associated with the method below it, correct me if I'm wrong
如果你注释掉 nslog,条件会与它下面的方法相关联,如果我错了,请纠正我
回答by Abizern
The answers you have are correct for your question. But. Your real question is how to turn of NSLogs in certain conditions. i.e. you want them to show for Debug builds, and not for Release builds. In which case try defining and using the DLog()macro as described on Cocoa Is My Girlfriend. If you're using Xcode4 it's even easier because the Debugand Releasebuilds define and undefine DEBUGso you don't have to do that.
你的答案对你的问题是正确的。但。您真正的问题是如何在某些条件下关闭 NSLogs。即您希望它们显示用于调试版本,而不是用于发布版本。在这种情况下,请尝试定义和使用Cocoa Is My Girlfriend 中DLog()所述的宏。如果您使用的是 Xcode4,那就更容易了,因为and构建定义和取消定义,所以您不必这样做。DebugReleaseDEBUG
It's a lot easier than commenting and uncommenting lines of code.
这比注释和取消注释代码行要容易得多。
回答by Sean S Lee
#ifdef RELEASE
#define NSLog(...) do { } while (0)
#endif
is the best way i found so far.
是我目前找到的最好的方法。
回答by Jignesh Chanchiya
#define NSLog(...)
#define NSLog(...)
add this line into your .pch file
将此行添加到您的 .pch 文件中
if you want log than comment it
如果你想记录而不是评论它
回答by Ed Liss
I would do this
我会这样做
#define EnableNSLog 1
#if EnableNSLog == 0
#define NSLog //
#elif EnableNSLog == 1
#warning Disable NSLog
#endif
This should generate a warning message to remind me to disable NSLogbefore final release.
这应该会生成一条警告消息,提醒我NSLog在最终发布之前禁用。
回答by EmptyStack
You can do this in a single find and replace operation. You can just do this simple Regular Expressionreplace. This handles both the commented(//) and non-commented lines. This also works even if the previous commented lines has more than two forward slashes(like ///)instead of rwo. You can refer this link. Do the following steps.
您可以在单个查找和替换操作中完成此操作。你可以做这个简单的正则表达式替换。这处理注释(//)和非注释行。即使前面的注释行有两个以上的正斜杠(如 ///)而不是 rwo,这也有效。你可以参考这个链接。执行以下步骤。
- Select Edit > Find > Find and Replace in Workspace
- Style=> Regular Expression
- Type
(/)*(NSLog.*)in Findfield. - Do the findoperation.
- Type
//\2in the Replacefield. - Do the replaceoperation.
- 选择“编辑”>“查找”>“在工作区中查找和替换”
- 样式=>正则表达式
(/)*(NSLog.*)在查找字段中键入。- 执行查找操作。
//\2在替换字段中键入。- 执行替换操作。
Enjoy the beauty of regular expressions;-)
享受正则表达式之美;-)
回答by teriiehina
in the Menu bar : Edit > Find > Find and Replace in Workspace then, display options to use regular expressions. search/replace for "[^/]NSLog"
在菜单栏中:编辑 > 查找 > 在工作区中查找和替换,然后显示使用正则表达式的选项。搜索/替换“[^/]NSLog”

