混合Swift和Objective C:“ProjectName-Swift.h”文件未找到 [英] Mixing Swift and Objective C : "ProjectName-Swift.h" file not found

查看:283
本文介绍了混合Swift和Objective C:“ProjectName-Swift.h”文件未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在开发一个包含Swift和Objective-C类的iOS项目。

So I'm working on an iOS project that contains Swift and Objective-C Classes.

要实例化Objective-C中描述的对象,我明白了我需要有一个Bridging-Header文件,其中包含我将在Swift类中使用的所有头文件的导入。它在我的项目中非常有用。
我创建了我需要的所有类,我在Swift中继承了Objective-C类,一切都还可以。

To instantiate an object described in Objective-C, I've understand that I needed to have a Bridging-Header file that contains all the imports of the headers I will have to use on my Swift classes. And it works great on my project. I made all classes I needed, I subclassed Objective-C classes in Swift, everything is OK.

我的问题是在相反的情况下:在Objective-C文件中实例化一个swift对象。所以我读到我需要有这些选项:

My problem is when it comes to do the opposite: instantiate a swift object in an Objective-C file. So I read that I need to have those options :


  • 定义模块

  • 在* .m文件中添加行 #import<#YourProjectName#> -Swift.h我正在努力。

  • Define Modules as Yes
  • add the line #import "<#YourProjectName#>-Swift.h" on the *.m file I'm working on.

这就是我所做的:

我更改了需要更改的构建设置上的所有值。我添加了 import 行,我甚至尝试创建头文件,但我仍然有<#YourProjectName#> -Swift。 h找不到文件错误。

I changed all the values on the Build Settings that needed to be changed. I added the import line, I even tried to create the header file, but I still have the "<#YourProjectName#>-Swift.h" file not found error.

当然,我用实际的模块名称替换了YourProjectName,我在下找到的名称构建设置的打包部分中的产品模块名称。另外,项目名称中没有空格。

Of course, I replaced the YourProjectName by the actual module name, the one I found under Product Module Name in Packaging section on Build Settings. Also, there is no spaces in the Project Name.

我忘记了一步吗?

谢谢你你的帮助。

推荐答案

确保输入 -Swift.h 来自.m Objective-C文件的文件,而不是.h头文件。 docs 只讨论从.m导入:

Make sure you are importing the -Swift.h file from a .m Objective-C file and not a .h header file. The docs only talk about importing from .m:


...将Swift代码的Xcode生成的头文件导入任何目标 - C .m文件要使用你的Swift代码。

... import the Xcode-generated header file for your Swift code into any Objective-C .m file you want to use your Swift code from.

-Swift.h 文件,我发现从.h文件导入它总是失败,即使我知道该文件存在。因为它是由构建生成的,所以我猜它是在编译器筛选标题时清理掉的,并在编译.swift文件后重新生成。

The -Swift.h file is generated during the build and I've found that importing it from a .h file always fails, even when I know the file exists. Since it's generated by the build, I'm guessing it's cleaned out when the compiler is sifting through the headers and regenerated once the .swift files are compiled.

这篇关于混合Swift和Objective C:“ProjectName-Swift.h”文件未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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