Xcode / iOS:如何确定代码是否在 DEBUG / RELEASE 版本中运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9063100/
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
Xcode / iOS: How to determine whether code is running in DEBUG / RELEASE build?
提问by P i
I am making an app that processes sensitive credit card data.
我正在制作一个处理敏感信用卡数据的应用程序。
If my code is running in debug mode I want to log this data to the console and make some file dumps.
如果我的代码在调试模式下运行,我想将此数据记录到控制台并进行一些文件转储。
However on the final appstore version (ie when it is running in release mode) it is essential all of this is disabled (security hazard)!
然而,在最终的 appstore 版本中(即当它在发布模式下运行时),必须禁用所有这些(安全隐患)!
I will try to answer my question as best I can; so the question becomes 'Is this solution path the right or best way to do it?'
我会尽量回答我的问题;所以问题变成了“这个解决方案是正确的还是最好的方法?”
// add `IS_DEBUG=1` to your debug build preprocessor settings
#if( IS_DEBUG )
#define MYLog(args...) NSLog(args)
#else
#define MYLog(args...)
#endif
回答by Damo
Check your project's build settings under 'Apple LLVM - Preprocessing', 'Preprocessor Macros' for debug to ensure that DEBUG
is being set - do this by selecting the project and clicking on the build settings tab. Search for DEBUG
and look to see if indeed DEBUG
is being set.
在“Apple LLVM - 预处理”、“预处理器宏”下检查项目的构建设置以进行调试以确保DEBUG
正在设置 - 通过选择项目并单击构建设置选项卡来执行此操作。搜索DEBUG
并查看是否确实DEBUG
正在设置。
Pay attention though. You may see DEBUG changed to another variable name such as DEBUG_MODE.
不过要注意。您可能会看到 DEBUG 更改为另一个变量名称,例如 DEBUG_MODE。
then conditionally code for DEBUG in your source files
然后有条件地在源文件中为 DEBUG 编码
#ifdef DEBUG
// Something to log your sensitive data here
#else
//
#endif
回答by Jeehut
For a solution in Swift please refer to this threadon SO.
有关 Swift 中的解决方案,请参阅SO上的此线程。
Basically the solution in Swiftwould look like this:
基本上,Swift 中的解决方案如下所示:
#if DEBUG
println("I'm running in DEBUG mode")
#else
println("I'm running in a non-DEBUG mode")
#endif
Additionally you will need to set the DEBUG
symbol in Swift Compiler - Custom Flags
section for the Other Swift Flags
key via a -D DEBUG
entry. See the following screenshot for an example:
此外,您还需要通过条目DEBUG
在Swift Compiler - Custom Flags
部分中为Other Swift Flags
键设置符号-D DEBUG
。有关示例,请参见以下屏幕截图:
回答by Nick Lockwood
Apple already includes a DEBUG
flag in debug builds, so you don't need to define your own.
Apple 已经DEBUG
在调试版本中包含了一个标志,因此您无需定义自己的标志。
You might also want to consider just redefining NSLog
to a null operation when not in DEBUG
mode, that way your code will be more portable and you can just use regular NSLog
statements:
您可能还想考虑NSLog
在非DEBUG
模式下重新定义为空操作,这样您的代码将更具可移植性,您可以只使用常规NSLog
语句:
//put this in prefix.pch
#ifndef DEBUG
#undef NSLog
#define NSLog(args, ...)
#endif
回答by Qun Li
Most answers said that how to set #ifdef DEBUG and none of them saying how to determinate debug/release build.
大多数答案都说如何设置 #ifdef DEBUG 而没有人说如何确定调试/发布版本。
My opinion:
我的看法:
回答by Kirill Kudaev
Swiftand Xcode 10+
Swift和Xcode 10+
#if DEBUG
will pass in ANY development/ad-hoc build, device or simulator. It's only false for App Store and TestFlight builds.
#if DEBUG
将通过任何开发/临时构建、设备或模拟器。仅对 App Store 和 TestFlight 构建是错误的。
Example:
例子:
#if DEBUG
print("Not App Store build")
#else
print("App Store build")
#endif
回答by geowar
zitao xiong's answer is pretty close to what I use; I also include the file name (by stripping off the path of FILE).
zitao xiong 的回答与我使用的非常接近;我还包括文件名(通过剥离FILE的路径)。
#ifdef DEBUG
#define NSLogDebug(format, ...) \
NSLog(@"<%s:%d> %s, " format, \
strrchr("/" __FILE__, '/') + 1, __LINE__, __PRETTY_FUNCTION__, ## __VA_ARGS__)
#else
#define NSLogDebug(format, ...)
#endif
回答by Fa.Shapouri
In xcode 7, there is a field under Apple LLVM 7.0 - preprocessing, which called "Preprocessors Macros Not Used In Precompiled..." I put DEBUGin front of Debugand it works for me by using below code:
在Xcode 7,有下田间苹果LLVM 7.0 -预处理,这被称为“预处理器宏未使用预编译......”我把DEBUG前面调试,它通过使用下面的代码为我工作:
#ifdef DEBUG
NSString* const kURL = @"http://debug.com";
#else
NSString* const kURL = @"http://release.com";
#endif
回答by Vyacheslav
Just one more idea to detect:
还有一个想法要检测:
DebugMode.h
调试模式.h
#import <Foundation/Foundation.h>
@interface DebugMode: NSObject
+(BOOL) isDebug;
@end
DebugMode.m
调试模式.m
#import "DebugMode.h"
@implementation DebugMode
+(BOOL) isDebug {
#ifdef DEBUG
return true;
#else
return false;
#endif
}
@end
add into header bridge file:
添加到头桥文件:
#include "DebugMode.h"
#include "DebugMode.h"
usage:
用法:
DebugMode.isDebug()
DebugMode.isDebug()
It is not needed to write something inside project properties swift flags.
不需要在项目属性 swift 标志中写一些东西。
回答by Zitao Xiong
Not sure if I answered you question, maybe you could try these code:
不确定我是否回答了您的问题,也许您可以尝试以下代码:
#ifdef DEBUG
#define DLOG(xx, ...) NSLog( \
@"%s(%d): " \
xx, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__ \
)
#else
#define DLOG(xx, ...) ((void)0)
#endif