在Objective-C项目中找不到Swift类 [英] Swift classes not found in Objective-C project

查看:548
本文介绍了在Objective-C项目中找不到Swift类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Objective-C开发一个iOS项目。我需要在其中集成一些Swift文件。因此,执行以下操作:

I am working on an iOS project developed using Objective-C. I need to integrate some Swift files in it. So, did following:


  1. 添加了一个名为 temp的新Swift文件,以便 MyProject-Bridging-Header .h 是由我的Xcode IDE(版本9.1)添加的。

  2. 将我要使用的Swift文件添加到了项目中。

  3. 将目标设置定义模块更改为是。

  4. 类似地,产品模块名称设置为 我的项目。

  5. 我可以看到 MyProject-swift.h 的文件名,该文件名为 Objective-C生成的接口头名称 。

  6. 在我的一个Objective-C .m文件中为 myProject-swift.h 添加了导入语句,并且能够成功构建项目。

  7. 我在Finder中检查了是否已创建 myProject-swift.h
  8. 在Swift类声明中确保@obj前缀。

  1. Added a new Swift file named "temp", so that MyProject-Bridging-Header.h is added by my Xcode IDE (Version 9.1).
  2. Added the Swift files, which I want to use, to the project.
  3. Changed target setting Defines Module to YES.
  4. Similarly, "Product Module Name" is set to "MyProject".
  5. I can see the MyProject-swift.h a file name for the setting "Objective-C Generated Interface Header Name".
  6. Added import statement for myProject-swift.h in one of my Objective-C .m files and was able to build the project successfully.
  7. I checked in Finder that myProject-swift.h was created.
  8. Ensured the @obj prefix in the Swift classes declaration.

在我尝试使用时仍然任何一个Swift类,然后我会收到编译时错误未声明标识符的用户。

Still, when I try to use any of the Swift classes, then I get compile time error "User of undeclared identifier".

问题的原因可能是什么?我错过了什么吗?

What can be the reason for the issue? Am I missing something?

推荐答案

弄清楚了...

我在Objective-C类中缺少Swift类的前向声明。我所需要做的就是在 .h 文件的接口声明之前添加以下语句。

I was missing the forward declaration of the Swift class in my Objective-C class. All I needed to do was to add following statement before the interface declaration in the .h file.

@class MySwiftClassName;

添加该语句后,它将成功构建。

Once the statement was added, it's building successfully.

这篇关于在Objective-C项目中找不到Swift类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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