0

We have a suite of apps which share a set of common screens. Some screens have use more functionality in some apps than others. For example, in one app our issue screen supports tags, but in the others it doesn't. In one, our regions screen supports workflows, but in the others it doesn't.

We're using core data, and the different apps have different data models; If an entity isn't used by an app's data model, the corresponding class doesn't get built into that app.

The result of this is that some of our controllers contain references to classes that may or may not exist when the controller is used in a particular app. At the moment, we get around this using "magic" #defines in our apps' PCH files; so some code in our Issue controller is wrapped in #ifdef SGB_ISSUES_HAVE_TAGS and some code in our Regions controller is wrapped in #ifdef SGB_REGIONS_ARE_FILTERED_BY_WORKFLOW.

What I'd like is to do this instead by detecting whether a file has been included in the project. So at the top of our Issue controller I'd have something like this:

#if exists_in_project("SGBIssueTag.h")
#import "SGBIssueTag.h"
#endif

and then I could put #ifdef SGB_ISSUES_HAVE_TAGS into SGBIssueTag.h; any app we built that used the issue controller would get the issue tag functionality as soon as we included the IssueTag entity, and we wouldn't have to mess around with the PCH file.

Is anything like this possible?

4

1 回答 1

0

这个问题困扰了很多人——头文件对项目来说是“全局”的问题。

我的建议是在编译期间尝试强制错误。您可以添加 PCH 或包含在其中为每个应用程序定义的文件 - 假设您有应用程序 A、B 和 C。因此,在任何一个构建期间,只有其中一个处于活动状态。

然后,在每个 .m 文件(可能还有一些 .h 文件)中,您将有条件地包含标题:

...
#if defined(A) || defined(B)
#include "SomeInterfaceFile.h"
#endif
...

一旦开始构建 A,您将在源代码引用超出范围的头文件中定义的某些对象的文件中遇到编译错误。该代码还必须包含在类似的 if 语句中。

这将完美地工作,缺点是您的代码会被这些条件语句弄乱。

于 2013-10-23T14:47:51.937 回答