如何添加-Swift.h标头中使用的正向类引用? [英] How can I add forward class references used in the -Swift.h header?

查看:99
本文介绍了如何添加-Swift.h标头中使用的正向类引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  1. 一个名为MyTableViewController
  2. 的Objective-C类
  3. 一个名为DeletionWorkflow
  4. 的Objective-C类
  1. An Objective-C class called MyTableViewController
  2. 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代码中来使用此类,则MyTableViewControllerDeletionWorkflow都将出现未定义的符号错误.

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.hMyTableViewController.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屋!

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