ios 在没有预处理器宏的情况下,有没有办法在 Xcode 项目的项目级别定义实用的方案特定标志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24111854/
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
In absence of preprocessor macros, is there a way to define practical scheme specific flags at project level in Xcode project
提问by banDedo
Before swift I would define a set of schemes for alpha, beta, and distribution builds. Each of these schemes would have a set of macros that were defined to gate certain behaviors at the project level. The simplest example is the DEBUG=1 macro that is defined by default for all Xcode projects in the default scheme for the Run build. One could query #ifdef DEBUG ... and make decisions in the code accordingly, even compiling out non-necessary code.
在 swift 之前,我会为 alpha、beta 和分发版本定义一组方案。这些方案中的每一个都有一组宏,这些宏被定义为在项目级别控制某些行为。最简单的示例是 DEBUG=1 宏,默认情况下为 Run 构建的默认方案中的所有 Xcode 项目定义。人们可以查询 #ifdef DEBUG ... 并相应地在代码中做出决定,甚至编译出不必要的代码。
It seems that this type of configurational gating is not as easy using swift, as macros are not supported. Can someone suggest a comparable approach, I don't care if the code is compiled out, per se. I would like to gate features based on build scheme, though.
使用 swift 似乎这种类型的配置门控并不容易,因为不支持宏。有人可以提出类似的方法吗,我不在乎代码本身是否被编译出来。不过,我想根据构建方案对功能进行门限。
回答by Jean Le Moignan
In Swift you can still use the "#if/#else/#endif" preprocessor macros (although more constrained), as per Apple docs. Here's an example:
在 Swift 中,您仍然可以使用“#if/#else/#endif”预处理器宏(尽管受到更多限制),如Apple 文档所述。下面是一个例子:
#if DEBUG
let a = 2
#else
let a = 3
#endif
Now, you must set the "DEBUG" symbol elsewhere, though. Set it in the "Swift Compiler - Custom Flags" section, "Other Swift Flags" line. You add the DEBUG symbol with the -D DEBUG
entry.
现在,您必须在别处设置“DEBUG”符号。在“Swift Compiler - Custom Flags”部分的“Other Swift Flags”行中设置它。您在-D DEBUG
条目中添加 DEBUG 符号。
(Build Settings -> Swift Compiler - Custom Flags)
(构建设置 -> Swift 编译器 - 自定义标志)
As usual, you can set a different value when in Debug or when in Release.
像往常一样,您可以在 Debug 或 Release 中设置不同的值。
I tested it in real code; it doesn't seem to be recognized in a playground.
我在实际代码中对其进行了测试;在操场上似乎无法识别它。
回答by Logan
We ran into an issue with not wanting to set swift compiler flags because we didn't want to have to set them and keep them up to date for different targets etc. Also, in our mixed codebase, we didn't want to make remember to set our flags appropriately all the time for each language.
我们遇到了不想设置 swift 编译器标志的问题,因为我们不想必须设置它们并使它们保持最新以用于不同的目标等。此外,在我们的混合代码库中,我们不想记住始终为每种语言适当地设置我们的标志。
For ours, we declared a file in ObjC
对于我们的,我们在 ObjC 中声明了一个文件
PreProcessorMacros.h
PreProcessorMacros.h
extern BOOL const DEBUG_BUILD;
In the .m
在他们之中
PreProcessorMacros.m
PreProcessorMacros.m
#ifdef DEBUG
BOOL const DEBUG_BUILD = YES;
#else
BOOL const DEBUG_BUILD = NO;
#endif
Then, in your Objective-C Bridging Header
然后,在你的 Objective-C 桥接标题中
#import "PreProcessorMacros.h"
Now, use this in your Swift codebase
现在,在你的 Swift 代码库中使用它
if DEBUG_BUILD {
println("debug")
} else {
println("release")
}
This is definitely a workaround, but it solved our problem so I posted it here in the hopes that it will help. It is not meant to suggest that the existing answers are invalid.
这绝对是一种解决方法,但它解决了我们的问题,所以我把它贴在这里希望它会有所帮助。这并不意味着现有的答案是无效的。
回答by Sahil Kapoor
More swifty solution to Logans method. Set -D DEBUG
in Other Swift Flags
of Swift Compiler - Custom Flags
section in build settings of your target.
Logans 方法的更快速解决方案。坐落-D DEBUG
在Other Swift Flags
的Swift Compiler - Custom Flags
在你的目标的构建设置部分。
Then declare following method in global scope:
然后在全局范围内声明以下方法:
#if DEBUG
let isDebugMode = true
#else
let isDebugMode = false
#endif
Now use it as
现在将其用作
if isDebugMode {
// Do debug stuff
}
回答by ghostatron
I'm working in a mixed language code base where the obj-c code uses a macro to send debug messages to the console (and that macro relies on our debug preprocessor flag). I wanted to be able to call that same macro in the swift code...
我正在使用混合语言代码库,其中 obj-c 代码使用宏将调试消息发送到控制台(并且该宏依赖于我们的调试预处理器标志)。我希望能够在 swift 代码中调用相同的宏...
- I created a class method on one of my obj-c classes that is a wrapper around that macro.
- I added that obj-c header to our bridge header file.
- Now my swift code calls that class method as a "proxy" to the obj-c macro.
- 我在我的一个 obj-c 类上创建了一个类方法,它是该宏的包装器。
- 我将该 obj-c 头文件添加到我们的桥头文件中。
- 现在,我的 swift 代码将该类方法称为 obj-c 宏的“代理”。
It's mildly annoying that I can't just call the macro straight up in the swift code, but at least now I only have one place in the project to worry about turning my debug flag on/off.
我不能直接在 swift 代码中直接调用宏有点烦人,但至少现在我在项目中只有一个地方可以担心打开/关闭我的调试标志。
回答by Marjan Basiri
For me, set the debug item of "Active Compilation Condition" to "DEBUG" worked.
对我来说,将“ Active Compilation Condition”的调试项设置为“DEBUG”即可。
Then using DEBGU key work in #IF DEBUG works in debug mode and #ELSE in release mode:
然后在 #IF DEBUG 中使用 DEBGU 密钥工作在调试模式下工作,#ELSE 在发布模式下工作:
- Select your target,
- In Build Setting tab search for "Active Compilation Condition",
- Set the value of its "Debug" item to "YourKeyWord",
Use simply as follow:
#if DEBUG print("You'r running in DEBUG mode!") #else print("You'r running in RELEASE mode!") #endif
- 选择你的目标,
- 在“构建设置”选项卡中搜索“活动编译条件”,
- 将其“调试”项的值设置为“YourKeyWord”,
简单使用如下:
#if DEBUG print("You'r running in DEBUG mode!") #else print("You'r running in RELEASE mode!") #endif