构建一个Swread框架,引用Objective-C代码 [英] Building a Swift framework with references to Objective-C code

查看:73
本文介绍了构建一个Swread框架,引用Objective-C代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个用Swift编写的iOS项目,我想从常规的app项目中取出一些类,然后将它捆绑到一个可重用的Swift框架中(也许可以在Github上公开发布)通过Cocoapods)。

I'm working on an iOS project written in Swift, and I would like to take some classes out of the regular "app" project, and bundle it into a reusable Swift framework instead (and maybe make it publicly available on Github and via Cocoapods).

我遇到了问题,因为框架似乎不能有Objective-C桥接头,但为了编译我的框架代码,我需要引用几个Objective-C类(在这种情况下:Google Maps iOS SDK)。

I'm running into issues because frameworks seemingly can't have Objective-C bridging headers, but in order to compile my framework code, I need to reference several Objective-C classes (in this case: the Google Maps iOS SDK).

我还在我的框架项目中添加了GoogleMaps.framework作为链接库,但是,如何从Swift代码中导入它?

I've also added GoogleMaps.framework as a linked library in my framework project, but then, how can I "import" it from Swift code?

这是否可以使用当前工具和Swift版本,我该如何处理?

Is this even possible with the current tools and Swift version, and how should I proceed?

谢谢。

推荐答案

这并不复杂,实际上......我只是做错了。

It wasn't that complicated, actually... I was just doing some things wrong.

首先,该套接字需要桥接头 g:Google Maps iOS SDK是作为常规.framework文件提供的,因此开发语言对如何在Swift中导入它没有影响。 Apple在文档中明确提到了这一点: https://developer.apple.com/ library / ios / documentation / Swift / Conceptual / BuildingCocoaApps / MixandMatch.html ,导入外部框架。

First, bridging headers are not required in that setting: the Google Maps iOS SDK is provided as a regular .framework file, so the development language has no impact on how it can be imported in Swift. Apple clearly mentions it in the documentation: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html, "Importing external frameworks".

这就像添加框架一样简单链接二进制文件库项目设置部分。不要忘记还添加依赖库和框架(在GoogleMaps.framework的情况下,有很多)。

It's as easy as adding the framework to the "Link binary with libraries" section of the project settings. Do not forget to also add depending libraries and frameworks (in GoogleMaps.framework's case, there are quite a few).

然后,在Swift代码中,框架类应该是只需这样做:

Then, in Swift code, the framework classes should be available simply by doing:

import GoogleMaps

没有桥接标题,没有处理非模块标题等错误。

No bridging header, no dealing with "non-modular header etc." errors.

这篇关于构建一个Swread框架,引用Objective-C代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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