将Objective-C框架暴露给Swift

时间:2016-12-14 10:18:05

标签: ios objective-c swift

我正在Objective-C中编写一个Cocoa Touch静态库。它将通过Cocoapods分发,使用它的开发人员也应该能够在Swift中执行此操作。 在我的测试项目中,库可以在Objective-C中完美地使用,但在Swift中我得到编译器错误:Defines Module

在图书馆目标YES设置为"abc"。根据Apple指南,这是唯一必须做的事情:https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

1 个答案:

答案 0 :(得分:0)

在现有的swift项目中实现pch文件:

  

档案>新>文件...(适用于您的案例的macOS应用程序)

add pch file

然后您可以在pch文件中导入这样的Objective-C库(test.pch是我的文件)

#ifndef test_pch
#define test_pch

#import <YourLib/YourLibMainClass.h>

// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.

#endif /* test_pch */

现在你需要告诉你的项目有一个pch文件。 转到目标构建设置并搜索“前缀标题”。

Apple LLVM 8.0 - 语言部分为前缀标题键添加值(小心pch文件项目路径)

enter image description here

您可以在swift文件中导入您的库

import YouLibrary