简体   繁体   English

如何将 Cocoapods 与 Swift 项目集成?

[英]How to integrate Cocoapods with a Swift project?

当 Apple 推出他们的新编程语言Swift 时,我想知道如何将它与通过CocoaPods提供的现有 Objective-C 库集成

Cocoapods 0.36 and above introduces the use_frameworks! Cocoapods 0.36 及以上版本引入了use_frameworks! instruction which implies that the bridging header is not required for importing Objective-C pods in Swift.这意味着在 Swift 中导入 Objective-C pod 不需要桥接头。

Please find below a full example using MBProgressHUD and Alamofire :请在下面找到使用MBProgressHUDAlamofire的完整示例:

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.从桥接头中删除#imports,如果不需要,甚至可以删除桥接头文件。 If you choose the latter possibility, do not forget to delete the path (to this deleted bridging header file) in your Xcode project configuration.如果您选择后一种可能性,请不要忘记删除 Xcode 项目配置中的路径(到这个已删除的桥接头文件)。

3. Adding imports 3. 添加导入

Add import MBProgressHUD and/or import Alamofire at the top of every Swift files that need these class(es).在需要这些类的每个 Swift 文件的顶部添加import MBProgressHUD和/或import Alamofire

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 枚举,你会遇到比你更大的问题。

( Source of this answer ) 这个答案的来源

For your information: I guess (you will have to test by yourself to be sure) that the use_frameworks!供您参考:我猜(您必须自己测试以确保) use_frameworks! instruction in your Podfile is only compatible with Xcode projects targeting iOS >= 8. Podfile 中的指令仅与针对 iOS >= 8 的 Xcode 项目兼容。

It seems that the process is similar to the one described in Mix and Match section of Using Swift with Cocoa and Objective-C documentation.该过程似乎类似于在使用 Swift 与 Cocoa 和 Objective-C文档的混合和匹配部分中描述的过程。

  1. Create your Podfile and run pod install .创建您的 Podfile 并运行pod install
  2. Create a new Objective-C header file, Example-Bridging-Header.h , and add it to the project.创建一个新的 Objective-C 头文件Example-Bridging-Header.h ,并将其添加到项目中。
  3. Add import statement to the bridge header .将 import 语句添加到桥头
  4. Set Objective-C Bridging Header for your target:为你的目标设置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更多信息: 将 Cocoapods 与 Swift 项目集成

UPDATE : CocoaPods 0.36 stable version has been released.更新:CocoaPods 0.36 稳定版已经发布。 It officially supports Swift.它正式支持 Swift。


CocoaPods now supports Swift in their latest 0.36 release. CocoaPods 现在在最新的 0.36 版本中支持 Swift。 It's still in beta but it works.它仍处于测试阶段,但它可以工作。

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.但是,如果您想通过 CocoaPods 将用 Objective-C 编写的库添加到 Swift 项目中,则有一个问题。 You need to add the line use_frameworks!您需要添加一行use_frameworks! to your Podfile.到您的 Podfile。 Here's an example.这是一个例子。

use_frameworks!
platform :ios, '8.0'

pod 'MagicalRecord'

I wrote a blog post regarding this as well.我也写了一篇关于这个的博客文章

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 项下执行此操作

Now you can use cocoapods 0.36.0 version by running sudo gem install cocoapods which supports to integrate swift frameworks .现在您可以通过运行支持集成 swift 框架的sudo gem install cocoapods来使用 cocoapods 0.36.0 版本。 When you use a framework written by swift, you should explicit use it in Podfile:当你使用 swift 编写的框架时,你应该在 Podfile 中显式使用它:

platform :ios, '8.0'
use_frameworks!
pod 'Alamofire'

就我而言,我发现我没有将 pod 添加到主要目标,而是仅添加到 Podfile 中的 Test 和 TestUI 目标,如here所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM