Skip to content
DEBUGBUILD
Swift Objective-C
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
DEBUGBUILD.xcodeproj
DEBUGBUILD
images
LICENSE
README.md

README.md

Debug 模式与 Release 模式的区分


区分 Debug 模式与 Release 模式有两种方法: ####第一种方法:

在 (Build Settings -> Swift Compiler - Custom Flags) 中加入 -DDEBUGStackoverflow

但是在 NSHipster 中有提到不推荐此方法 Avoiding -DDEBUG in Swift


####第二种方法

通过 Preprocessor Macros (预处理宏命令) 来区分模式。


1.新建一个 PreProcessorMacros.h 文件,代码如下

#import <Foundation/Foundation.h>

@interface PreProcessorMacros : NSObject

#ifndef PreProcessorMacros_h
#define PreProcessorMacros_h

extern BOOL const DEBUG_BUILD;

#endif

@end


@implementation PreProcessorMacros

#ifdef DEBUG
    BOOL const DEBUG_BUILD = YES;
#else
    BOOL const DEBUG_BUILD = NO;
#endif

@end

2.在 Bridged Header: #import "PreProcessorMacros.h"

也可直接把步骤一的代码放进 Bridged Header


3.测试

if DEBUG_BUILD {
    print("It's Debug build")
} else {
    print("It's Release build")
}

⌘⇧< 进入下图可模式切换


原理

关键的代码在:

#ifdef DEBUG
    BOOL const DEBUG_BUILD = YES;
#else
    BOOL const DEBUG_BUILD = NO;
#endif

此处的 DEBUG 究竟来自哪?

进入 'Build Settings' -> 搜索 'Preprocessor Macros',真相大白


抽取于干货极多的:Reader Submissions - New Year's 2016 🍺

You can’t perform that action at this time.