如何在 Swift 项目中使用 Objective-C CocoaPods
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31884507/
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 use Objective-C CocoaPods in a Swift Project
提问by shaydawg
Is there a way I can use a CocoaPod written in Objective-C in my Swift project using swift?
有没有办法可以在我的 Swift 项目中使用 swift 使用用 Objective-C 编写的 CocoaPod?
Do I just make a bridging header? And if so, can I access the objects, classes, and fields defined by the libraries in the CocoaPod in Swift?
我只是做一个桥接头吗?如果是这样,我可以在 Swift 中访问 CocoaPod 中的库定义的对象、类和字段吗?
回答by Vlad Papko
Basic answer to your question is Yes, you can use objective-c code built with CocoaPods.
您问题的基本答案是,您可以使用使用 CocoaPods 构建的 Objective-c 代码。
More important question is "How to use such libs?"
Answer on this question depends on use_frameworks!flag in your Podfile:
Let's imagine that you want use Objective-C pod with name CoolObjectiveCLib.
更重要的问题是“如何使用这样的库?”
这个问题的答案取决于use_frameworks!您的标志Podfile:
让我们假设您想使用带有 name 的 Objective-C pod CoolObjectiveCLib。
If your pod file uses use_frameworks!flag:
如果您的 pod 文件使用use_frameworks!标志:
// Podfile
use_frameworks!
pod 'CoolObjectiveCLib'
Then you don't need add any bridge header files.
Everything that you need is import framework in Swift source file:
那么你不需要添加任何桥头文件。
您需要的一切都是 Swift 源文件中的导入框架:
// MyClass.swift
import CoolObjectiveCLib
Now you can use all classes that are presented in lib.
现在您可以使用 lib 中提供的所有类。
If your pod file doesn't use use_frameworks!flag:
如果您的 pod 文件不使用use_frameworks!标志:
// Podfile
pod 'CoolObjectiveCLib'
Then you need create bridging header file and import there all necessary Objective-C headers:
然后你需要创建桥接头文件并在那里导入所有必要的 Objective-C 头文件:
// MyApp-Bridging-Header
#import "CoolObjectiveCLib.h"
Now you can use all classes that are defined in imported headers.
现在您可以使用在导入的头文件中定义的所有类。
回答by RodolfoNeto
In podFile use the flag use_frameworks!
Inside Xcode in the Podfolder structure in the dependency, you add xxxxxxx-umbrella.hin Support Files.
在 podFile 中使用标志use_frameworks!在Pod依赖项的文件夹结构中的Xcode 内,添加xxxxxxx-umbrella.h支持文件。
In your {PROJECT_NAME}-Bridging-Header.huse:
在您的{PROJECT_NAME}-Bridging-Header.h使用中:
#import "xxxxxxx/xxxxxxx-umbrella.h"
It works for me.
这个对我有用。
回答by o0sea0o
AND don't forget to add Bridging Header file name to Target -> Build Settings -> Objective-C Bridging Header
并且不要忘记将桥接头文件名添加到 Target -> Build Settings -> Objective-C Bridging Header
回答by Lucian Boboc
You just need a bridging header and import there what you need.
您只需要一个桥接头并在那里导入您需要的内容。

