ios 在objective-c中导入swift类,找不到<myModule>-Swift.h文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32098696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:20:23  来源:igfitidea点击:

import swift class in objective-c, <myModule>-Swift.h file not found

iosobjective-cxcodeswift

提问by user842225

I have an iOS project written with Objective-C. I created an Swift class in the project, the bridging header file for accessing objective-c in Swift is generated successfully, and it works fine.

我有一个用 Objective-C 编写的 iOS 项目。我在项目中创建了一个Swift类,在Swift中访问objective-c的桥接头文件生成成功,运行正常。

My problem is the other way around. I want to import Swift class in objective-c code.

我的问题是反过来。我想在 Objective-c 代码中导入 Swift 类。

In xcode, target -> Build Settings--> Swift Compilersection, I see Objective-C Generated Interface Header Namefield with value myModule-Swift.h, but when I import this header in my objective-cclass:

在 xcode 中,target -> Build Settings--> Swift Compiler部分,我看到Objective-C Generated Interface Header Name了 value 字段myModule-Swift.h,但是当我在Objective-c类中导入此标头时:

#import "myModule-Swift.h"

I get compiler error:

我收到编译器错误:

myModule-Swift.h file not found

and in project, I cannot find this file either. How can I solve this problem?

在项目中,我也找不到这个文件。我怎么解决这个问题?

My xcode version is 6.4

我的 xcode 版本是 6.4

回答by Daniel Krom

Updated May 2018 Xcode 9.3

2018 年 5 月更新 Xcode 9.3



  1. Build Settings->Objective-C Generated Interface Header Name
    and set the value to YourModule-Swift.h (this is usually already set, this is the filename you need to import on .mfile #import "YourModule-Swift.h"
  1. Build Settings->Objective-C Generated Interface Header Name
    并将值设置为 YourModule-Swift.h(这通常已经设置,这是您需要在.m文件上导入的文件名#import "YourModule-Swift.h"

(Example, Project named CData)

(例如,名为 的项目CData

Example

例子

  1. Same as Step 1, Go to Build Settings and search for "Defines Module", set both values to YES

  2. Create a class that extends NSObjecton .swiftfile

  1. 与步骤 1 相同,转到 Build Settings 并搜索“Defines Module”,将两个值都设置为 YES

  2. 创建一个NSObject.swift文件上扩展的类

Example

例子

  1. Build the project again

  2. Import YourModule-Swift.hfile on .mfile (Please notice it's case sensitive, Mymodule!== MyModule)

  1. 再次构建项目

  2. YourModule-Swift.h.m文件上导入文件(请注意区分大小写,Mymodule!== MyModule

enter image description here

在此处输入图片说明

回答by Karol Kulesza

In case anybody is wondering why after spending hours on this issue it is still unresolved, you might be in a situation similar to mine, where I was actually developing a framework, rather than an app.

如果有人想知道为什么在这个问题上花了几个小时仍然没有解决,你可能和我的情况类似,我实际上是在开发一个框架,而不是一个应用程序。

In such case, the format of the import statement for Objective-C Generated Interface Headeris as follows:

在这种情况下,Objective-C Generated Interface Header的import语句格式如下:



#import <ModuleName/ModuleName-Swift.h>

回答by killerz

OMG.. the actual import statement was not "class-Swift.h" but rather "projectname-Swift.h"

OMG .. 实际的导入语句不是“class-Swift.h”而是“projectname-Swift.h”

You can find the name of the file if you look under build settings->Swift Compiler Code Generation -> Objective-C Generated Interface Header Name

如果您在构建设置-> Swift 编译器代码生成-> Objective-C 生成的接口头名称下查看,您可以找到该文件的名称

The file was not generated when I dragged in Swift source into the GUI. Only when I right-clicked->Add file to "project". It then asked to generate the header files.

当我将 Swift 源代码拖入 GUI 时,未生成该文件。只有当我右键单击->将文件添加到“项目”时。然后它要求生成头文件。

回答by ThorstenC

My addition to Daniel Kroms answer:

我对 Daniel Kroms 的补充回答:

  • Never add -Swift.h Header to header. Even if it seems to work. Add the Import to .m file only!

  • In case you use in your header swift classes, make a forward declaration with @class swiftclassname before your @interface

  • 永远不要将 -Swift.h Header 添加到 header。即使它看起来有效。仅将导入添加到 .m 文件!

  • 如果您在标题 swift 类中使用,请在 @interface 之前使用 @class swiftclassname 进行前向声明

Then you will see your real errors in your code.

然后你会在你的代码中看到你真正的错误。

回答by Genki

For me, the problem was that I had bitcode on. When I clicked on the "Update to recommended project settings", it changed a few settings which probably the culprit. I turned "Enabled Bitcode" to "No" in the Build Settings and it is fixed now.

对我来说,问题是我打开了位码。当我单击“更新到推荐的项目设置”时,它更改了一些可能是罪魁祸首的设置。我在构建设置中将“启用位码”改为“否”,现在已修复。

enter image description here

在此处输入图片说明

回答by Kaz Yoshikawa

I was stacked this for a quite a while. In my case, my target name is something like "my-app" using dash as a part of target name. I tried to #import "my-app-Swift.h", but Xcode kept giving me errors.

我被这个堆放了很长一段时间。就我而言,我的目标名称类似于“my-app”,使用破折号作为目标名称的一部分。我尝试过#import "my-app-Swift.h",但 Xcode 一直给我错误。

I dug under 'DerivedData' folder and I found "my_app-Swift.h". So if you are using some interesting characters for the target name. You may try replace those with underscore _.

我在“DerivedData”文件夹下挖掘,发现“my_app-Swift.h”。因此,如果您使用一些有趣的字符作为目标名称。您可以尝试用下划线替换它们_

回答by Travis Tubbs

I ran into the same issue yesterday and worked for hours to fix it with no avail. Others may have been in the same boat as I. I did all of the steps described above, but nothing worked.

我昨天遇到了同样的问题,并工作了几个小时来解决它,但无济于事。其他人可能和我在同一条船上。我执行了上述所有步骤,但没有任何效果。

The cause of mine breaking was because of project name artifacts all over my project (from previously changing its name improperly).

地雷破坏的原因是因为我的项目中到处都是项目名称工件(以前不正确地更改了名称)。

If all of the above steps fail, I would suggest doing like I did and renaming your project so that Xcode can reset somethings... That solved the problem for me. Doing so worked like a charm!

如果上述所有步骤都失败了,我建议像我一样重命名您的项目,以便 Xcode 可以重置某些内容......这为我解决了问题。这样做很有魅力!

回答by Igor Maia Romcy

In my case, I have forgotten to check a swift framework to the target/classes that I was using it, really specifically case but it may help someone in the future.

就我而言,我忘记检查我正在使用它的目标/类的 swift 框架,真的很特别,但它可能会在将来对某人有所帮助。

回答by rsc

For me the solution was to create a new target. For an unknown reason, the target that I had didn't have that "Swift Compiler - General" settings and thus no to "Objective-C Generated Interface Header Name" field. Having that field specified in the project was not enough.

对我来说,解决方案是创建一个新目标。由于未知原因,我的目标没有“Swift Compiler - General”设置,因此没有“Objective-C Generated Interface Header Name”字段。在项目中指定该字段是不够的。

回答by Alfonso Tesauro

This is not an exact answer but more of a workaround, but can save you time in some difficult cases. If you, as suggested by some of the the previous answers, can actually find the swift header buried inside the derived data folder, you are allowed to import it using the full path. This is not specific to the swift header, instead it can be applied to any header. Hope you can find this answer useful.

这不是一个确切的答案,而是更多的解决方法,但可以在某些困难的情况下为您节省时间。如果按照前面的一些答案的建议,您实际上可以找到隐藏在派生数据文件夹中的 swift 标头,则可以使用完整路径导入它。这不是特定于 swift 标头的,而是可以应用于任何标头。希望你能发现这个答案很有用。