gpt4 book ai didi

ios - 将 Objective-C 框架暴露给 Swift

转载 作者:行者123 更新时间:2023-11-28 08:21:38 24 4
gpt4 key购买 nike

我正在用 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文件:

File > New > File... (macOS app for your case)

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 文件。转到您的目标build设置并搜索“Prefix Header”。

Apple LLVM 8.0 - Language 部分为 Prefix header 键添加值(注意你的 pch 文件项目路径)

enter image description here

您可以将您的库导入到一个 swift 文件中

import YouLibrary

关于ios - 将 Objective-C 框架暴露给 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41140047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com