目标C中的公共和私有伞头 [英] Public and Private umbrella header in Objective C

查看:85
本文介绍了目标C中的公共和私有伞头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码库,是Objective C和Swift的结合.在某些地方,Swift使用Objective,反之亦然.我现在需要基于此代码库创建一个框架,但不想在我的伞形标头中包含所有目标c文件.这是我的问题:

在我的框架内,我仍然需要能够使用objc中的swift,反之亦然;但是不希望公开swift类在内部使用的所有objc文件.框架中不允许桥接标头,因此swift所需的所有标头都需要放入伞状标头中.

我想知道是否有可能将内部swift代码所需的所有objc标头放入一个文件,该文件将是我的私有伞头,而我需要公开的所有文件都将进入公共伞头. >

有什么建议吗?

解决方案

对于Objective-C-> Swift案例,我已经成功使用显式声明的模块作为此问题的一种解决方案.我没有将模块声明分成单独的私有模块映射,而是在同一模块映射中声明了框架模块和显式模块,因为对该问题的评论之一引起了关注(我不确定是否或如何可以在同一框架内使用私有模块映射生成的标头).

这是我为MPFoundation.framework定义的模块映射的摘录,其中包括一个显式模块MPManuscriptCompiler_Protected,该模块导入了标头"MPManuscriptCompiler+Protected.h",该标头不包括在框架的伞形标头中:

framework module MPFoundation {
    umbrella header "MPFoundation.h"

    export *
    module * { export * }

    explicit module MPManuscriptCompiler_Protected {
        header "MPManuscriptCompiler+Protected.h"
        export *
    }
}

然后,我在Swift子类中使用此显式模块MPManuscriptCompiler_Protected,该子类存在于同一框架中,如下所示:

import MPFoundation.MPManuscriptCompiler_Protected

从技术上讲,我的解决方案实际上只是一种解决方法:要使此方法有效,可以在框架中将"MPManuscriptCompiler+Protected.h"标记为私有或项目级标题,因此它在伞状标题中将不可见,并且无法用于基于标头的导入及其文件名.因此,这可以解决必须在伞形标头中包含此标头的问题.

但是,以这种方式创建的模块已在框架中公开公开,可供不应该看到它的眼睛使用.我没有对此进行进一步的调查,因为实际上这已经很好地解决了这个问题(至今还没有解决我偶然将保护标头导入不应被导入的问题).

I have this code base which is Objective C and Swift mix. Some places Swift uses Objective and and vice versa. I need to create a framework now based of off this codebase but do not want to include all the objective c files in my umbrella header. Here's my problem:

Inside my framework I still need to be able to use swift from objc and vice versa; but do not want to expose all those objc files that are being used internally by swift classes. Bridging header is not allowed in frameworks so all the headers needed by swift need to go in umbrella header.

I'm wondering if it's possible to have all the objc headers needed by internal swift code go in a file which would be my private umbrella header and all the files that I need to expose would go in public umbrella header.

Any suggestions?

解决方案

I am successfully using explicitly declared modules as a solution of sorts for this issue for the Objective-C -> Swift case. I have not separated the module declaration into a separate private module map but declared both the framework module and an explicit module inside the same modulemap because of the concern raised in one of the comments to the question (I wasn't sure if or how it is possible to use the header generated by the private module map inside the same framework).

Here's an excerpt of the modulemap I have defined for my MPFoundation.framework, which includes an explicit module MPManuscriptCompiler_Protected that imports the header "MPManuscriptCompiler+Protected.h" which is not included in the umbrella header for the framework:

framework module MPFoundation {
    umbrella header "MPFoundation.h"

    export *
    module * { export * }

    explicit module MPManuscriptCompiler_Protected {
        header "MPManuscriptCompiler+Protected.h"
        export *
    }
}

I then use this explicit module MPManuscriptCompiler_Protected in my Swift subclass which is present in the same framework like so:

import MPFoundation.MPManuscriptCompiler_Protected

My solution is really technically just a workaround though: for this to work, "MPManuscriptCompiler+Protected.h" can be marked a private or project level header in the framework, so it will not be visible in the umbrella header and will not be available for header based imports with its filename. So, this works around having to include this header in the umbrella header.

However, the module created this way is publicly exposed in the framework and available for eyes that should not see it. I did not investigate this further as practically this solves the issue well enough (am yet to hit issues where I would have by accident imported that protected header where it wasn't supposed to be imported).

这篇关于目标C中的公共和私有伞头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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