xcode 如何在整个应用程序中禁用 NSLog?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16002001/
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 to disable NSLog all over the app?
提问by Timur Bernikovich
I want to disable NSLog()
across all instances in an app. I found some code that does that:
我想NSLog()
在应用程序中的所有实例中禁用。我发现了一些这样做的代码:
#ifndef DEBUG
#define NSLog //
#endif
But adding this code to each file isn't good idea. How can I make it easier?
但是将此代码添加到每个文件并不是一个好主意。我怎样才能让它更容易?
采纳答案by jszumski
Xcode has a precompiled header file ({project-name}-Prefix.pch
in the Supporting Files group by default) that is a great place to put code that will be used across every file in the project. For going a step further and improving the log message itself, see Is it true that one should not use NSLog() on production code?.
Xcode 有一个预编译的头文件({project-name}-Prefix.pch
默认情况下在 Supporting Files 组中),这是放置将在项目中的每个文件中使用的代码的好地方。要更进一步并改进日志消息本身,请参阅是否不应该在生产代码上使用 NSLog()?.
回答by Miguel Gallego
Add the following lines to your constants file:
将以下行添加到您的常量文件中:
#define DEBUGGING NO //or YES
#define NSLog if(DEBUGGING)NSLog
The two following sentences give the same results
以下两句给出相同的结果
#define NSLog
and
和
#define NSLog //
because all text that begin with // are deleted in a precompiling phase ("//" included)
因为在预编译阶段删除了所有以 // 开头的文本(包括“//”)
回答by Eyeball
This will also take care of the warnings that arise when using
这也将处理使用时出现的警告
#define NSLog //
The code:
编码:
#ifndef DEBUG
#define NSLog(...)
#endif
回答by matt
What I do is put this in the precompiled header file (YourAppName.pch):
我所做的是把它放在预编译的头文件(YourAppName.pch)中:
#define MyLog if(0); else NSLog
Then I change all NSLog to MyLog everywhere else in the app. It works as if it were NSLog. But when you want to disable NSLog, go back to the .pchfile and change the 0
to 1
and presto, all logging stops.
然后我将应用程序中其他所有地方的所有 NSLog 更改为 MyLog。它就像 NSLog 一样工作。但是当你想禁用 NSLog 时,回到.pch文件并更改0
to1
和 presto,所有日志记录都会停止。
回答by Rajeev Kumar Barnwal
Just add the following lines when you do'"t need NSLOG to your constants file
当您不需要 NSLOG 到您的常量文件时,只需添加以下几行
#define NSLog //
and comment it when you need it.
并在需要时发表评论。