我正在做一个处理敏感信用卡数据的应用程序。

如果我的代码在调试模式下运行,我想将这些数据记录到控制台并进行一些文件转储。

然而,在最终的appstore版本(即当它运行在发布模式时),必须禁用所有这些(安全隐患)!

我会尽力回答我的问题;所以问题就变成了“这个解决方案是正确的还是最好的方法?”

// add `IS_DEBUG=1` to your debug build preprocessor settings  

#if( IS_DEBUG )  
#define MYLog(args...) NSLog(args)  
#else  
#define MYLog(args...)  
#endif  

当前回答

为使用Kotlin多平台ios调试模式的人添加此功能。下面是确定构建是调试还是发布的方法。

if (Platform.isDebugBinary) {
     NSLog(message ?: "", "")
}

其他回答

在xcode 7中,在Apple LLVM 7.0下有一个字段-预处理,称为“Preprocessors Macros Not Used In Precompiled…” 我把DEBUG放在DEBUG前面,它为我使用下面的代码:

#ifdef DEBUG
    NSString* const kURL = @"http://debug.com";
#else
    NSString* const kURL = @"http://release.com";
#endif

熊子韬的答案和我用的很接近;我还包括文件名(通过剥离file的路径)。

#ifdef DEBUG
    #define NSLogDebug(format, ...) \
    NSLog(@"<%s:%d> %s, " format, \
    strrchr("/" __FILE__, '/') + 1, __LINE__, __PRETTY_FUNCTION__, ## __VA_ARGS__)
#else
    #define NSLogDebug(format, ...)
#endif

Swift + Xcode 14更新。

从Xcode 8开始,你应该使用“活动编译条件”而不是“其他Swift标志”。从Xcode 9.3开始,苹果已经为新项目包含了DEBUG值:

这意味着你不需要改变任何项目设置,可以简单地使用#if DEBUG立即:

#if DEBUG
  print("something")
#endif

为使用Kotlin多平台ios调试模式的人添加此功能。下面是确定构建是调试还是发布的方法。

if (Platform.isDebugBinary) {
     NSLog(message ?: "", "")
}

还有一个想法需要检测:

调试模式.h

#import <Foundation/Foundation.h>

@interface DebugMode: NSObject
    +(BOOL) isDebug;
@end

调试模式.m

#import "DebugMode.h"

@implementation DebugMode
+(BOOL) isDebug {
#ifdef DEBUG
    return true;
#else
    return false;
#endif
}
@end

添加到头桥文件:

# include“DebugMode.h”

用法:

DebugMode isDebug()。

不需要在项目属性swift标志中写入内容。