当 Swift 桥接头导入一个导入 Hopscotch-Swift.h 本身的文件时如何防止循环引用 [英] How to prevent circular reference when Swift bridging header imports a file that imports Hopscotch-Swift.h itself
问题描述
我正在将 Swift 集成到一个现有的大型 Objective C 项目中,并且遇到了我认为是循环引用的问题.
I am integrating Swift into a large existing Objective C project and have run into what I think is a circular reference.
有问题的类如下:
#import "Hopscotch-Swift.h"
@interface MyController : UIViewController<MyProtocol>
...
@end
Swift 协议
@objc protocol MyProtocol: NSObjectProtocol {
...
}
桥接标题
#import "MyController.h"
此代码无法编译,因为 Hopscotch-Swift.h
文件不会生成.
This code fails to compile because the Hopscotch-Swift.h
file will not generate.
我认为这是由于循环引用错误,因为我可以将 Hopscotch-Swift.h
导入到不包含在 Hopscotch-Bridging-Header.h中的目标 c 头文件中代码>,它工作正常.
I think this is due to a circular reference error as I can import Hopscotch-Swift.h
into objective c headers that are not included in Hopscotch-Bridging-Header.h
and it works fine.
是否有解决此问题的方法,或者我应该向 Apple 提交雷达报告?
Is there a workaround for this issue or should I file a radar with Apple?
推荐答案
在您的情况下,前向声明应该有效.
Forward declaration should work, in your case.
在您的 .h 中:
@protocol MyProtocol;
@interface MyController : UIViewController<MyProtocol>
@end
在您的 .m 中:
#import "HopScotch-Swift.h"
来自 如何我添加了 -Swift.h 头文件中使用的前向类引用? 和 Swift 互操作性指南:
From How can I add forward class references used in the -Swift.h header? and the Swift interoperability guide:
如果您在 Swift 代码中使用自己的 Objective-C 类型,请确保在将 Swift 生成的头文件导入到要访问 Swift 的 Objective-C .m 文件之前,为这些类型导入 Objective-C 头文件代码来自.
If you use your own Objective-C types in your Swift code, make sure to import the Objective-C headers for those types prior to importing the Swift generated header into the Objective-C .m file you want to access the Swift code from.
这篇关于当 Swift 桥接头导入一个导入 Hopscotch-Swift.h 本身的文件时如何防止循环引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!