Xcode项目如何检测目标程序或如何使用env vars [英] Xcode project how to detect target programatically or how to use env vars

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

问题描述

我想做一个应用程序测试,将一些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.

这是最令人沮丧的

推荐答案

不要介意...这是在Xcode / Cocoa dev中的一个积极的体验。

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项目如何检测目标程序或如何使用env vars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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