ios 如何将 Cocoapods 与 Swift 项目集成?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24122914/
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
How to integrate Cocoapods with a Swift project?
提问by Michal K.
采纳答案by Michal K.
It seems that the process is similar to the one described in Mix and Matchsection of Using Swift with Cocoa and Objective-Cdocumentation.
该过程似乎类似于在使用 Swift 与 Cocoa 和 Objective-C文档的混合和匹配部分中描述的过程。
- Create your Podfile and run
pod install
. - Create a new Objective-C header file,
Example-Bridging-Header.h
, and add it to the project. - Add import statement to the bridge header.
- Set
Objective-C Bridging Header
for your target:
- 创建您的 Podfile 并运行
pod install
. - 创建一个新的 Objective-C 头文件,
Example-Bridging-Header.h
并将其添加到项目中。 - 将 import 语句添加到桥头。
- 设置
Objective-C Bridging Header
为你的目标:
Now you can use your library, in that case, MKUnits, in your Swift file:
现在你可以在你的 Swift 文件中使用你的库,在这种情况下,MKUnits:
let kilograms = NSNumber.mass_kilogram(2)()
let pounds = NSNumber.mass_pound(10)()
let result = kilograms.add(pounds)
println(result)
More here: Integrating Cocoapods with a Swift project
回答by King-Wizard
Cocoapods 0.36 and aboveintroduces the use_frameworks!
instruction which implies that the bridging header is not required for importing Objective-C pods in Swift.
Cocoapods 0.36 及更高版本引入的use_frameworks!
指令暗示在 Swift 中导入 Objective-C pod 不需要桥接头。
Please find below a full example using MBProgressHUDand Alamofire:
请在下面找到使用MBProgressHUD和Alamofire的完整示例:
1. Podfile
1. Podfile
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.3'
use_frameworks!
pod 'Alamofire', '>= 1.2.2' # Swift pod
pod 'MBProgressHUD', '>= 0.9.1' # Objective-C pod
2. Deletion
2. 删除
Remove the #imports from your bridging header or even delete the bridging header file if you do not need it. If you choose the latter possibility, do not forget to delete the path (to this deleted bridging header file) in your Xcode project configuration.
从桥接头中删除#imports,如果不需要,甚至可以删除桥接头文件。如果您选择后一种可能性,请不要忘记删除 Xcode 项目配置中的路径(到这个已删除的桥接头文件)。
3. Adding imports
3. 添加导入
Add import MBProgressHUD
and/or import Alamofire
at the top of every Swift files that need these class(es).
添加import MBProgressHUD
和/或import Alamofire
在需要这些类的每个 Swift 文件的顶部。
4. Fix the enums if necessary
4. 必要时修复枚举
You're now using bona fide frameworks, so your enums have moved in flight! You might have a line of Swift that was fine with the bridging header like this:
您现在正在使用真正的框架,因此您的枚举已在飞行中移动!您可能有一行 Swift 可以很好地处理桥接头,如下所示:
progressHUD.mode = MBProgressHUDModeIndeterminate
That now has to become this:
现在必须变成这样:
progressHUD.mode = MBProgressHUDMode.Indeterminate
Not to big a deal, but the pile of errors might lead you astray that you have a bigger problem than you do if you are using a lot of Objective-C enums.
没什么大不了的,但是一堆错误可能会让你误入歧途,如果你使用大量的 Objective-C 枚举,你会遇到比你更大的问题。
(这个答案的来源)
For your information: I guess (you will have to test by yourself to be sure) that the use_frameworks! instruction in your Podfile is only compatible with Xcode projects targeting iOS >= 8.
供您参考:我猜(您必须自己测试以确保) use_frameworks! Podfile 中的指令仅与针对 iOS >= 8 的 Xcode 项目兼容。
回答by Isuru
UPDATE: CocoaPods 0.36 stable version has been released. It officially supports Swift.
更新:CocoaPods 0.36 稳定版已经发布。它正式支持 Swift。
CocoaPods now supports Swift in their latest 0.36 release. It's still in beta but it works.
CocoaPods 现在在最新的 0.36 版本中支持 Swift。它仍处于测试阶段,但它可以工作。
First you need to install the CocoaPods beta (currently beta 2) by running this in your Terminal.
首先,您需要通过在终端中运行它来安装 CocoaPods beta(目前是 beta 2)。
sudo gem install cocoapods --pre
That's pretty much it. You can add Swift libraries like you would do normally.
差不多就是这样。您可以像往常一样添加 Swift 库。
But there's a catch if you want to add a library written in Objective-C to a Swift project via CocoaPods. You need to add the line use_frameworks!
to your Podfile. Here's an example.
但是,如果您想通过 CocoaPods 将用 Objective-C 编写的库添加到 Swift 项目中,则有一个问题。您需要将该行添加use_frameworks!
到您的 Podfile 中。这是一个例子。
use_frameworks!
platform :ios, '8.0'
pod 'MagicalRecord'
I wrote a blog postregarding this as well.
回答by Michelle
If you are getting a file not found error in your bridging-header.h, you might want to make sure your Pods library is being linked in your Build Scheme.
如果您在 bridging-header.h 中遇到文件未找到错误,您可能需要确保您的 Pods 库已链接到您的构建方案中。
The CocoaPods troubleshooting section describes how to do this under item #4 here
CocoaPods 故障排除部分描述了如何在此处的第 4 项下执行此操作
回答by Allen
Now you can use cocoapods 0.36.0 version by running sudo gem install cocoapods
which supports to integrate swift frameworks . When you use a framework written by swift, you should explicit use it in Podfile:
现在你可以通过运行sudo gem install cocoapods
它来使用 cocoapods 0.36.0 版本,它支持集成 swift 框架。当你使用 swift 编写的框架时,你应该在 Podfile 中显式使用它:
platform :ios, '8.0'
use_frameworks!
pod 'Alamofire'