如何在 Objective-C 中导入 Swift 协议

How to import Swift protocol in Objective-C

在我的 swift class 我有下一个协议:

@objc
public protocol MyDelegate: AnyObject { ... }

我需要从 header 文件访问该协议,而不是 .m 文件。 我该怎么做?

我在 SO 中发现的情况很少,我尝试了它们

#import <ModuleName/ModuleName-Swift.h>

@import MySwiftClass;

但我仍然无法在 Obj-C 的 .h 文件中使用该协议。 我该如何解决这个问题?

.h 文件中生成的 header 引用 Swift class 或协议会创建循环引用,因此这是不可能的。 Apple 建议使用前向声明并在 .m 文件中进行实际导入。

Source

When declarations in an Objective-C header file refer to a Swift class or protocol that comes from the same target, importing the generated header creates a cyclical reference. To avoid this, use a forward declaration of the Swift class or protocol to reference it in an Objective-C interface.

换句话说,你想要的(至少目前)是不可能的。