xcode 如何识别 Swift 是否是使用优化编译的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25916396/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:36:54  来源:igfitidea点击:

How to identify if Swift was compiled with Optimization

xcodeswift

提问by Jeef

As some of you may be aware when running in fully Debug-Mode swift can be terribly slow. Is there a way i can print out a message in code or to the GUI to let me know if I somehow forgot to compile it correctly. I'm running in mixed mode so if somebody can give me Objc and Swift code that would be super awesome.

正如你们中的一些人可能知道,在完全调试模式下运行时,swift 可能会非常慢。有没有办法可以在代码或 GUI 中打印出一条消息,让我知道我是否以某种方式忘记了正确编译它。我在混合模式下运行,所以如果有人能给我 Objc 和 Swift 代码,那将是非常棒的。

Thanks!

谢谢!

回答by jou

I don't think you can detect this at runtime, but you can use the DEBUGpreprocessor macro (in Objective-C) that is defined in the Debug configuration by default:

我认为您无法在运行时检测到这一点,但您可以使用DEBUG默认情况下在 Debug 配置中定义的预处理器宏(在 Objective-C 中):

#ifdef DEBUG
NSLog(@"I'm in debug mode!");
#endif

This assumes that you don't compile without optimizations in the Release configuration :-)

这假设您在发布配置中没有优化就不会编译:-)

If you want to check that in Swift, you need to define a Build Configurationby adding -D DEBUGto "Other Swift Flags" for the Debug configuration onlyin the Build settings. Then you can check for that configuration if #if:

如果您想在 Swift 中进行检查,则需要通过在构建设置中为调试配置添加到“其他 Swift 标志”来定义构建配置。然后您可以在以下情况下检查该配置:-D DEBUG#if

#if DEBUG
println("I'm in debug mode!")
#endif

回答by Charlie Monroe

You can use Xcode's schemes to add a flag as an argument or in the environment variables - you can then check for it using NSProcessInfo- either -argumentsor -environment.

您可以使用 Xcode 的方案将标志添加为参数或环境变量 - 然后您可以使用NSProcessInfo--arguments-environment.

In Xcode, go to Product > Scheme > Edit Scheme in the menu bar, select Run and under the Arguments tab, add either the argument or environment variable.

在 Xcode 中,转到菜单栏中的 Product > Scheme > Edit Scheme,选择 Run 并在 Arguments 选项卡下,添加参数或环境变量。