如何添加-Swift.h标头中使用的正向类引用? [英] How can I add forward class references used in the -Swift.h header?
问题描述
我正在将Swift代码集成到一个大型的Objective-C项目中,但是当我的Swift代码引用Objective-C类时,我遇到了问题.例如,假设我有:
I'm integrating Swift code into a large Objective-C project, but I'm running into problems when my Swift code refers to Objective-C classes. For example, suppose I have:
- 一个名为
MyTableViewController
的Objective-C类
- 一个名为
DeletionWorkflow
的Objective-C类
- An Objective-C class called
MyTableViewController
- An Objective-C class called
DeletionWorkflow
我声明了一个Swift类,如下所示:
I declared a Swift class as follows:
class DeletionVC: MyTableViewController {
let deleteWorkflow: DeletionWorkflow
...
}
如果我现在尝试通过将ProjectName-Swift.h
导入到Objective-C代码中来使用此类,则MyTableViewController
和DeletionWorkflow
都将出现未定义的符号错误.
If I now try to use this class by importing ProjectName-Swift.h
into Objective-C code, I get undefined symbol errors for both MyTableViewController
and DeletionWorkflow
.
我可以在导入ProjectName-Swift.h
之前先导入DeletionWorkflow.h
和MyTableViewController.h
来解决单个源文件中的问题,但这并不能扩展到我希望我的Swift和Objective-C进行交互的大型项目经常.
I can fix the problem in that individual source file by importing DeletionWorkflow.h
and MyTableViewController.h
before I import ProjectName-Swift.h
but this doesn't scale up to a large project where I want my Swift and Objective-C to interact often.
是否有一种方法可以向ProjectName-Swift.h
添加前向类引用,以便在我尝试在应用程序中使用Objective-C代码中的Swift类时不会发生这些错误?
Is there a way to add forward class references to ProjectName-Swift.h
so that these errors don't occur when I try to use Swift classes from Objective-C code in my app?
推荐答案
This is a little silly, but it sounds like your "workaround" is what Apple intended, at least for now. From the interoperability guide:
如果您在Swift代码中使用自己的Objective-C类型,请确保在将Swift生成的标头导入要访问的Objective-C
.m
文件之前,先导入这些类型的Objective-C标头.来自的Swift代码.
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.
在此devforums线程中,有人提到他们已经在Radar中提交了一个错误. 您可能也应该这样做.
In this devforums thread, someone mentioned they already filed a bug in Radar. You probably should too.
这篇关于如何添加-Swift.h标头中使用的正向类引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!