问题描述
我正在使用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:
- 添加了一个名为 temp的新Swift文件,以便
MyProject-Bridging-Header .h
是由我的Xcode IDE(版本9.1)添加的。 - 将我要使用的Swift文件添加到了项目中。
- 将目标设置
定义模块
更改为是。 - 类似地,产品模块名称设置为 我的项目。
- 我可以看到
MyProject-swift.h
的文件名,该文件名为 Objective-C生成的接口头名称 。 - 在我的一个Objective-C .m文件中为
myProject-swift.h
添加了导入语句,并且能够成功构建项目。 - 我在Finder中检查了是否已创建
myProject-swift.h
。 - 在Swift类声明中确保@obj前缀。
- Added a new Swift file named "temp", so that
MyProject-Bridging-Header.h
is added by my Xcode IDE (Version 9.1). - Added the Swift files, which I want to use, to the project.
- Changed target setting
Defines Module
to YES. - Similarly, "Product Module Name" is set to "MyProject".
- I can see the
MyProject-swift.h
a file name for the setting "Objective-C Generated Interface Header Name". - Added import statement for
myProject-swift.h
in one of my Objective-C .m files and was able to build the project successfully. - I checked in Finder that
myProject-swift.h
was created. - 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类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!