[英]Expose Objective-C Framework to Swift
我正在Objective-C中编写一个Cocoa Touch静态库。 它将通过Cocoapods分发,使用它的开发人员也应该能够在Swift中执行此操作。 在我的测试项目中,库可以在Objective-C中完美地使用,但是在Swift中我得到编译器错误: No such module MyLibrary
。
在库目标Defines Module
中设置为YES
。 根据Apple指南,这是唯一必须做的事情: https : //developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
在现有的swift项目中实现pch文件:
文件>新建>文件...(适用于您的案例的macOS应用程序)
然后你可以在你的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 - 语言部分为Prefix标题键添加值(小心你的pch文件项目路径)
您可以在swift文件中导入库
import YouLibrary
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.