Objective C 生成的界面 header 无法在其他 header 中导入

Objective C interface generated header can't be imported in other header

我不是 Obj C 专业人士,但我应该在 Objective C 中编写一些代码并将 Swift 代码连接到它。 我成功地将生成的 Header 导入到 .m 文件中:

#import "<my_module>-Swift.h"

但是当我尝试将相同的 header 导入 .h 文件时,它会抛出此错误:

顺便说一句,我只想添加一个 public 变量,该变量从 Swift class 实例化到特定对象 c class。我试图将这些行放在 .h 和 .m 文件中:

@property (nonatomic, readwrite, strong) Card *card;

我该怎么办?

如果您想在 Objective-C header 中引用 Swift class,您不能 #import *-Swift.h 文件,但是应该使用 https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html 中描述的前向声明,请参阅标题为 Swift Class 或 Objective-C Header 中的协议的部分。这基本上就是@jtbandes 在评论中的建议。

这里有个问题:你想在 Objective-C 中使用的 Swift class 必须直接或间接扩展 NSObject class。

在你的例子中,因为你需要做的就是声明一个 Card* 类型的 属性,你实际上不需要导入 header——你可以 forward-declare class 和 @class Card; 再使用它。