以编程方式确定 iOS 项目中的当前目标(运行或测试) [英] Programmatically determine current target (run or test) in iOS project

查看:17
本文介绍了以编程方式确定 iOS 项目中的当前目标(运行或测试)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发 iOS 应用程序时,有没有办法以编程方式确定您是在 Test 目标中运行代码还是在常规 Run 目标中运行代码?

Is there any way to programmatically determine if you are running code in the Test target vs the regular Run target while developing iOS applications?

我有检查这个变量是否为零的技巧,因为它只在我的测试目标中,但这看起来很hacky.

I have the hack of checking if this variable is nil or not as it is only in my test target but this seems pretty hacky.

[[[NSProcessInfo processInfo] environment] objectForKey:@"XCInjectBundle"]

推荐答案

您应该在目标设置中为预处理器宏"定义适当的值.

You should define proper value for "Preprocessor Macros" in Target Settings.

在运行时你可以用ifdef语句来检查.

At the run time you can check it with ifdef sentence.

这篇关于以编程方式确定 iOS 项目中的当前目标(运行或测试)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆