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

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

问题描述

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

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

我的代码如何知道它是作为测试的一部分运行还是作为正常运行的一部分?只是通过某种方式知道我们是否处于测试目标"?因为该应用程序启动时会启动一堆请求,以使用来自服务器的信息填充我的coredata.在测试期间,我不希望它这样做.我想启动该应用程序,从文件中读取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"]

使用带有预处理器宏的构建设置DID对我不起作用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项目如何以编程方式检测目标或如何使用环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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