简体   繁体   English

检测iOS应用程序是否在没有预处理器宏的情况下运行Debug或Release版本,以进行单元测试?

[英]Detect whether iOS app is running Debug or Release build without preprocessor macros, for unit testing purposes?

I've seen plenty of answers and am familiar with using something preprocessor macros to check whether I'm in debug or release mode, but I'm keen to unit test around those scenarios, so I'd love a way to check that that I could mock. 我已经看到了很多答案,并且熟悉使用预处理器宏来检查我是处于调试模式还是发布模式,但是我很想在这些情况下进行单元测试,所以我想要一种方法来检查那个我可以嘲笑。

Can we detect debug, ad hoc or release without using an #if defined ? 我们可以在不使用#if defined情况下检测调试,临时或发布吗?

You can have a key configuration in info.plist with value ${CONFIGURATION} . 您可以在info.plist中使用值${CONFIGURATION}进行密钥configuration Make sure path for your plist file is set in you project build settings. 确保在项目构建设置中设置plist文件的路径。 See images below for reference. 请参阅以下图片以供参考。

plist图像

info.plist链接图像

You can then access infoDictionary to get the configuration. 然后,您可以访问infoDictionary来获取配置。 Code is below 代码如下

let bundle : NSBundle = NSBundle.mainBundle()
let configuration : String = (bundle.infoDictionary!["configuration"] as! String)  // configuration is key you set in plist file

configuration will be a string storing build configuration set in your scheme. configuration将是一个字符串,用于存储方案中的构建配置集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM