xcode 构建一个引用 Objective-C 代码的 Swift 框架
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28815487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Building a Swift framework with references to Objective-C code
提问by Romain
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).
我正在开发一个用 Swift 编写的 iOS 项目,我想从常规的“应用程序”项目中取出一些类,并将其捆绑到一个可重用的 Swift 框架中(并且可能在 Github 和 Cocoapods 上公开提供) )。
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).
我遇到了一些问题,因为框架似乎没有 Objective-C 桥接头,但是为了编译我的框架代码,我需要引用几个 Objective-C 类(在这种情况下:Google Maps iOS SDK)。
I've also added GoogleMaps.framework as a linked library in my framework project, but then, how can I "import" it from Swift code?
我还在我的框架项目中添加了 GoogleMaps.framework 作为链接库,但是,我如何从 Swift 代码“导入”它?
Is this even possible with the current tools and Swift version, and how should I proceed?
使用当前的工具和 Swift 版本甚至可以做到这一点,我应该如何进行?
Thanks.
谢谢。
采纳答案by Romain
It wasn't that complicated, actually... I was just doing some things wrong.
其实没那么复杂……我只是做错了一些事情。
First, bridging headers are notrequired 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".
首先,桥接头未在该环境中必需的:谷歌地图iOS版SDK是作为一个普通.framework文件,因此开发一种语言都有它如何能在斯威夫特进口没有任何影响。Apple 在文档中清楚地提到了它:https: //developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html,“导入外部框架”。
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).
就像将框架添加到项目设置的“Link binary with libraries”部分一样简单。不要忘记添加依赖库和框架(在 GoogleMaps.framework 的情况下,有很多)。
Then, in Swift code, the framework classes should be available simply by doing:
然后,在 Swift 代码中,框架类应该可以通过执行以下操作来使用:
import GoogleMaps
No bridging header, no dealing with "non-modular header etc." errors.
没有桥接头,不处理“非模块化头等”。错误。