在Swift项目中禁用NSLog进行生产 [英] Disabling NSLog For Production In Swift Project
问题描述
所以这个答案我是否需要在发布前禁用NSLog Application?提供了一种在生产环境中禁用NSLog的好方法,但不幸的是,这个解决方案似乎不适用于Swift项目。我的方法是将以下代码放在我用于项目中某些pod的桥接头.h文件中。
So this answer Do I need to disable NSLog before release Application? gives a great way to disable NSLog in a production environment, but unfortunately, this solution does not seem to work for Swift projects. My approach was to place the following code in the bridging header .h file that I am using for some pods in my project.
#ifdef DEBUG
#define DLog(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__])
#else
#define DLog(...) do { } while (0)
#endif
但是,在Swift中使用DLog代码导致编译器声明它们是未识别的符号。还有其他地方我应该放置这个 #ifdef
或者一般来说Swift项目是否有不同的解决方案?
However, using DLog in Swift code is causing the compiler to state that they are unrecongnized symbols. Is there somewhere else I should be placing this #ifdef
or is there a different solution for Swift project in general?
推荐答案
您需要设置编译器标志以使用Swift预处理器 - 转到Build Settings的 Swift编译器 - 自定义标志部分以设置 -D DEBUG
flag:
You'll need to set up a compiler flag to use the Swift preprocessor - go to the Swift Compiler - Custom Flags section of Build Settings to set up a -D DEBUG
flag:
然后在你的代码中你可以定义一个 DLog()
功能,只有在设置 DEBUG
标志时才打印您的信息:
Then in your code you can define a DLog()
function and only print your message if the DEBUG
flag is set:
func DLog(message: String, function: String = #function) {
#if DEBUG
println("\(function): \(message)")
#endif
}
这篇关于在Swift项目中禁用NSLog进行生产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!