Xcode 项目如何以编程方式检测目标或如何使用 env vars [英] Xcode project how to detect target programmatically or how to use env vars

查看:47
本文介绍了Xcode 项目如何以编程方式检测目标或如何使用 env vars的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个应用程序测试,解析一些 json,存储到核心数据,并读出一些对象.

I want to do an Application test that parses some json, stores to core data, and reads out some objects.

我的代码如何知道它是作为测试还是正常运行的一部分运行的?只是通过某种方式知道我们是否在测试目标中"?因为应用程序现在启动时会启动一堆请求,以使用来自服务器的信息填充我的核心数据.我不希望它在我的测试中这样做.我想启动应用程序,从文件中读取 HARDCODED json 并使用与其他方法相同的方法将其存储到 coredata 中,然后验证结果.

How can my code know if it's being run as part of a test or normal run? Just some way to know "are we in test target"? Because the app when it fires up now kicks off a bunch of requests to populate my coredata with info from the server. I don't want it to do this during my tests. I want to fire up the App, read HARDCODED json from a file and store this using the same methods as otherwise into coredata, and verify the results.

如果有人能解释如何在每个目标的基础上传递特定的键值对,并且可以从应用程序中读取,我会更高兴.

If someone could explain how to pass specific key-value pairs on a per target basis that can be read from within the app, I would be even more delighted.

推荐答案

没关系...发现是在方案"中设置的.

Never mind... figured out that it is in "Schemes" that you set this.

例如,如果您希望 TARGET=TEST 在测试期间可用,而 TARGET=RUN 在运行期间显示,只需在您的方案 > 环境变量 > 名称/值中设置即可.

For example if you want TARGET=TEST to be available during Test and TARGET=RUN to show during run, just set that in your Scheme > Environment Variables > Name/Value.

然后您可以从您的应用中执行以下操作:

Then from your app you can do:

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

使用带有预处理器宏的构建设置对我不起作用 b/c 我的测试目标(用于应用程序/集成测试)依赖于我的主要(非测试)目标,所以首先构建主要目标,这就是运行,即使您在运行的目标中定义的宏之后,您最终也会得到主目标预处理器宏.如果我在这里遗漏了什么,请随时解释.

Using build settings with preprocessor macros DID NOT work for me b/c my test target (for application/integration testing) is dependent on my main (not test) target, so the main target is built first and that's what runs, and you end up with main target preprocessor macros even though you are after the ones defined in the target you ran. If I missed something here someone feel free to explain please.

这篇关于Xcode 项目如何以编程方式检测目标或如何使用 env vars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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