问题描述
有没有办法在我的Swift项目中使用swift使用用Objective-C编写的CocoaPod?
Is there a way I can use a CocoaPod written in Objective-C in my Swift project using swift?
我只是制作桥接头吗?如果是这样,我可以在Swift中访问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?
推荐答案
问题的基本答案是,可以使用CocoaPods内置的Objective-C代码.
Basic answer to your question is Yes, you can use objective-c code built with CocoaPods.
更重要的问题是如何使用此类库?"
关于此问题的答案取决于您Podfile
中的use_frameworks!
标志:
假设您要使用名称为CoolObjectiveCLib
的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
.
如果您的Pod文件使用use_frameworks!
标志:
If your pod file uses use_frameworks!
flag:
// Podfile
use_frameworks!
pod 'CoolObjectiveCLib'
然后,您无需添加任何桥头文件.
您需要的一切都是Swift源文件中的导入框架:
Then you don't need add any bridge header files.
Everything that you need is import framework in Swift source file:
// MyClass.swift
import CoolObjectiveCLib
现在您可以使用lib中提供的所有类.
Now you can use all classes that are presented in lib.
如果您的Pod文件不使用use_frameworks!
标志:
If your pod file doesn't use use_frameworks!
flag:
// Podfile
pod 'CoolObjectiveCLib'
然后,您需要创建桥接头文件,并在其中导入所有必需的Objective-C头文件:
Then you need create bridging header file and import there all necessary Objective-C headers:
// MyApp-Bridging-Header
#import "CoolObjectiveCLib.h"
现在,您可以使用在导入的标头中定义的所有类.
Now you can use all classes that are defined in imported headers.
这篇关于如何在Swift项目中使用Objective-C CocoaPods的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!