Swift桥接标头导入导入Hopscotch-Swift.h本身的文件时,如何防止循环引用 [英] How to prevent circular reference when Swift bridging header imports a file that imports Hopscotch-Swift.h itself

查看:117
本文介绍了Swift桥接标头导入导入Hopscotch-Swift.h本身的文件时,如何防止循环引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将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

快速协议

@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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆