在Xcode 6中没有创建Objective-C header。

时间:2022-09-07 07:45:23

I have recently been working to add Swift to an existing project, to get to try it out in a real-world fashion.

我最近一直在为一个现有的项目添加Swift,以在现实世界中试用它。

Upon adding a Swift source file to the project, I have no problems about getting the "Bridging Header", that is, Objective-C to Swift.

在向项目中添加一个Swift源代码文件时,我没有遇到“桥接头”(即Objective-C, Swift)的问题。

But the -Swift.h header file that is supposed to expose Swift classes either marked @objc or subclasses of ObjC classes, is nowhere to be found :-(

但斯威夫特。h头文件,它应该暴露Swift类的标记@objc或ObjC类的子类,没有找到:-(

I don't see any specific instructions on how to accomplish the usage of my new subclass, written in Swift, in my main app code (which is still Obj-C).

在我的主要应用程序代码中,我没有看到任何关于如何使用我的新子类使用的具体说明(仍然是objc - c)。

The app that I am lead developer of has a fairly large codebase (70.000 lines), so transitioning it in one go is out of the question.

我所开发的应用程序有一个相当大的代码库(70.000行),所以在一个过程中转换它是不可能的。

31 个解决方案

#1


152  

Now it works.

现在它的工作原理。

  1. Project must have a Product Module Name that does not include spaces.
  2. 项目必须有一个不包含空格的产品模块名称。
  3. Defines Module must be set to Yes in Build Settings, under Packaging.
  4. 定义模块必须设置为Yes在构建设置中,在包装下。

Finally works. Thanks to everyone for the help :-)

最后的作品。感谢大家的帮助:-)

#2


121  

I had a similar problem and found that you can only add

我有一个类似的问题,发现你只能添加。

#import “ProductModuleName-Swift.h”

#进口“ProductModuleName-Swift.h”

to obj-c .m files, not .h files for the umbrella header to be found

要找到objc .m文件,而不是。h文件。

#3


52  

I found that I had to fix all build errors before it would generate the file.

我发现,在生成文件之前,我必须修复所有的构建错误。

The problem for me was that it was a chicken/egg problem, in that I didn't see any build errors until I'd actually commented out the #import statement:

我的问题是,这是一个鸡/蛋的问题,因为我没有看到任何构建错误,直到我实际上注释掉了#import语句:

//#import "ProductModuleName-Swift.h"

/ / #进口“ProductModuleName-Swift.h”

which revealed a bunch of other errors in my Swift code.

在我的Swift代码中显示了许多其他错误。

Once I fixed these new errors and got the source building successfully, I uncommented out the #import and bingo! The header was created and importing correctly :)

一旦我修复了这些新的错误,并且成功地获得了源构建,我就没有注释掉#import和bingo!标题的创建和导入正确:)

#4


41  

If your project module name has spaces in it, you must replace the spaces with an underscore.

如果项目模块名称中有空格,则必须用下划线替换空格。

For instance, if your project name is "My Project", you would use:

例如,如果您的项目名称是“我的项目”,您将使用:

#import "My_Project-Swift.h"

#进口“My_Project-Swift.h”

#5


39  

If you're like me you've probably got the header name wrong. After bashing my head for a while I looked for the file in DerivedData and sure enough it's there. On my setup (using the standard derived data folder, I believe):

如果你像我一样,你可能把标题写错了。在敲打我的脑袋一段时间后,我寻找文件,并确定它在那里。在我的设置(使用标准的派生数据文件夹,我相信):

cd ~/Library/Developer/Xcode/DerivedData
find * -iname '*Swift.h'

Will find it. If nothing in that folder matches then Xcode is not generating it.

会找到它的。如果该文件夹中没有匹配,那么Xcode就不会生成它。

I'm using Xcode Version 6.2 (6C86e)

我使用的是Xcode 6.2 (6C86e)

#6


30  

* The only important thing is: *

唯一重要的是:*。

to use the defined "Product Module Name" in the target, followed by -Swift.h

在目标中使用定义的“产品模块名称”,然后是-Swift.h。

#import <Product Module Name>-Swift.h

// in each ObjectiveC .m file having to use swift classes
// no matter in which swift files these classes sit.

No matter if "Defines Module" param is set to Yes or No or if "Product Module Name" Project is not set.

无论“定义模块”param是否设置为Yes或No,或“产品模块名称”项目未设置。

Reminder: Swift classes must deriving from NSObject or been tagged with @objc attribute in order to be exposed to ObjectiveC / Foundation || Cocoa ...

提示:Swift类必须从NSObject派生或被标记为@objc属性,以便暴露于ObjectiveC / Foundation || Cocoa…

#7


23  

I wanted to add one more reason you might find an issue with this - I was creating a framework that mixed Swift and Objective-C code. I was not able to import the Swift classes outside the framework - I checked for the -Swift.h file and it was being generated but was empty.

我想增加一个你可能会发现这个问题的原因——我正在创建一个框架,它混合了Swift和Objective-C代码。我无法在框架之外导入Swift类——我检查了-Swift。文件正在生成,但是是空的。

The problem turned out to be very, very simple - I had not declared any of my Swift classes public! As soon as I added the public keyword to the classes, I was able to use them from classes inside and outside the framework.

问题变得非常非常简单——我没有公开宣布我的任何一个快速课程。只要我将公共关键字添加到类中,我就能够在框架内和外部的类中使用它们。

Also of note, inside the framework (inside .m files only as another answer mentions) I had to import the -Swift.h file as:

另外,在框架内(在.m文件中只有另一个答案提到),我必须导入-Swift。h文件:

#import <FrameworkName/FrameworkName-Swift.h>

#8


13  

I had the same problem. Seems like you have to adjust the settings (Defines Module and Product Module Name) before you add your first Swift file.

我遇到了同样的问题。在添加第一个Swift文件之前,似乎必须调整设置(定义模块和产品模块名称)。

If you do it afterwards the "*-Swift.h" file will not be generated for this project even if you add further Swift files or delete the Swift file and create a new one.

如果你事后做,“*-Swift”。h“文件将不会为这个项目生成,即使你添加了更多的Swift文件或删除Swift文件并创建一个新的文件。

#9


12  

Here is another variation of the moduleName-Swift.h not being generated.

这是另一个变化的模块化swift。h不生成。

I decided to include IOS Charts in my project but did not want to mingle the sources in the same directory, so I placed the Charts Project folder next to my code's project folder. I dragged the Charts project into my Project's Navigator Bar and included the framework in the my project target's Embedded Binaries list in the General project settings and set the Embedded Content Contains Swift Code switch to yes in my project's Build Settings tab in the Build Options section.

我决定在我的项目中加入IOS图表,但不想在同一个目录中混合源代码,所以我将图表项目文件夹放在代码的项目文件夹旁边。我将图表项目拖到项目的导航栏中,并在项目目标的一般项目设置中包含了项目目标的嵌入式二进制列表中的框架,并设置嵌入的内容,其中包含了在构建选项部分中我的项目的Build settings选项卡中的Swift代码切换。

My project's moduleName-Swift.h file would never generate no matter what other switches or settings suggested here. Finally, using Lou Z's method of seeking out the -Swift.h files, I saw that a Charts-Swift.h file was being generated deep in my project's xcode Build directory in Charts.framework/Headers/

我的项目是moduleName-Swift。h文件无论在这里建议什么其他开关或设置,都不会生成。最后,运用卢茨的方法寻找-斯威夫特。h文件,我看到了一个图表。h文件正在我的项目的xcode构建目录中生成。

The solution to using Daniel Gindi's ios-charts Swift package without including the code in my project's source directory was to add:

在我的项目的源代码目录中,使用Daniel Gindi的ios-charts Swift包的解决方案是:

#import "Charts/Charts-Swift.h"

To the modules charting my project's data.

到模块中绘制我的项目的数据。

#10


12  

The file name is always preceded by your Target name. It is referred as Product name but practically it is the target name. So if you want it to build for a new target be ready to expect that_target-Swift.h file.

文件名称总是在您的目标名称前面。它被称为产品名称,但实际上它是目标名称。因此,如果你想让它为一个新目标而构建,那么就准备好期待这个目标- swift。h文件。

One way to handle this is

一种处理方法是。

  1. Add a preprocessor for each of your target that is the name of your target itself (without spaces). Ex. MY_TARGET=1. Add this in Project settings->Build Settings->Preprocessor Macros for each of your targets.
  2. 为每个目标添加一个预处理器,这是目标本身的名称(没有空格)。MY_TARGET = 1。在项目设置中添加这一点->构建设置->预处理器宏为每个目标。
  3. If you are using a PCH file,
  4. 如果你使用的是PCH文件,

Add these lines in the PCH file

在PCH文件中添加这些行。

#if MY_TARGET==1
#include "My_Target-Swift.h"
#elif THAT_TARGET==1
#include "That_Target-Swift.h" 
#endif

Advantage of using PCH file is that you don't have to include the headers everywhere.

使用PCH文件的优点是不需要在任何地方都包含头文件。

  1. If you are not using a PCH file, just add these same lines in a single header and include that header wherever you need to use the swift classes.
  2. 如果您没有使用PCH文件,只需在单个标头中添加这些相同的行,并在需要使用swift类的任何地方都包含该标题。

This should work just fine.

这应该没问题。

#11


10  

If XCode is actually generating your -Swift.h header (deep inside DerivedData) but it doesn't refer to your Swift classes, make sure you also have a bridging header defined. The way I read the docs implied I only needed that for calling Objective-C from Swift, but it seems to be necessary for calling Swift from Objective-C too.

如果XCode实际上生成了你的-Swift。h标头(deep inside DerivedData),但它不涉及您的Swift类,确保您也定义了一个桥接头。我阅读文档的方式暗示了我只需要从Swift调用Objective-C,但是从Objective-C中调用Swift似乎是必要的。

See my answer: https://*.com/a/27972946/337392

看到我的答案是:https://*.com/a/27972946/337392

EDIT: It is because of public vs. internal access modifiers, as I eventually found explained in the Apple docs:-

编辑:这是因为公共和内部访问修饰符,我最终在苹果文档中找到了解释:-。

By default, the generated header contains interfaces for Swift declarations marked with the public modifier. It also contains those marked with the internal modifier if your app target has an Objective-C bridging header.

默认情况下,生成的头包含与公共修饰符标记的Swift声明的接口。如果你的应用目标有一个Objective-C桥接头,它也会包含那些带有内部修饰符的标记。

#12


9  

Allow me to share my experiences trying to use Swift in an old objc project. I did not have to set Defines module to YES.

请允许我分享我在旧的objc项目中使用Swift的经验。我不需要设置定义模块为YES。

In my case I needed to manually make sure there was an objc Bridging Header. Only the generated interface header name was present in my build settings.

在我的例子中,我需要手动确保有一个objc连接头。在我的构建设置中只存在生成的接口标题名称。

在Xcode 6中没有创建Objective-C header。

This lead to a MyApp-Swift.h file to being generated, but without any traces of my Swift classes.

这就导致了MyApp-Swift。h文件生成,但是没有我的Swift类的任何痕迹。

The Apple documentation says that you will be prompted to create a bridging header when adding your first swift file. Well, I wasn't. I manually added a MyApp-Bridging-header.h file and pointed to it in the "Objective-C Bridging Header" field. That made my MyApp-Swift.h file become populated with my Swift classes.

苹果文档说,在添加第一个swift文件时,将提示您创建一个桥接头。好吧,我不是。我手动添加了一个myapp - bridgingheader。h文件并在“Objective-C桥接头”字段中指向它。这使我MyApp-Swift。h文件用我的Swift类填充。

Docs: Importing Swift into Objective-C

文档:将Swift导入Objective-C。

#13


6  

Seconding what a lot of people have here, but adding a pertinent screen shot. Swift and Obj-C code can certainly live together. It's not an all or none game.

这里有很多人的支持,但是增加了一个相关的屏幕截图。Swift和objc代码可以共存。这不是一场全部或一场比赛。

To access Swift files in your Objective-C, all you need to do is add this call to your Obj-C file (in the .m / implementation file):

要在Objective-C中访问Swift文件,需要做的就是将这个调用添加到objc - c文件中(在.m /实现文件中):

#import "{product_module_name}-Swift.h"

(Where {product_module_name} represents the product module name of your project). Rather than try to guess your product module name or figure out corner cases with spaces and special characters, just go to the build settings tab in the project and type in "product module name" - the inspector will reveal yours to you. Mine was something I did not expect it to be. Check out this screen shot if you're confused.

({product_module_name}表示项目的产品模块名称)。与其尝试猜测您的产品模块名称或计算出带有空格和特殊字符的角落案例,不如直接进入项目的build settings选项卡并输入“产品模块名称”——检查器将向您显示您的产品模块名称。我的想法是我没有预料到的。如果你感到困惑,看看这个屏幕截图。

在Xcode 6中没有创建Objective-C header。

And to get Obj-c code working in Swift, you just need to add a bridging header file and import the relevant Obj-C headers there.

为了让objc -c代码在Swift中工作,您只需添加一个桥接头文件并导入相关的object -c header。

#14


4  

The most important thing is that This file is invisible!!! At least it is in Xcode6 beta5. There will be no such file named "YourModule-Swift.h" in your workspace. Just make sure you have module name and defines module set to yes, and use it in your Objective-C class.

最重要的是这个文件是看不见的!!!至少在Xcode6 beta5。将不会有这样的文件命名为“YourModule-Swift”。h”在你的工作区。只需确保您拥有模块名称并将模块集定义为yes,并在Objective-C类中使用它。

#15


4  

This answer addresses the use-case where you may already have some Objective-C code that calls Swift classes and then you start receiving this error.

这个答案解决了用例,您可能已经有一些Objective-C代码调用Swift类,然后您开始接收这个错误。

How To Fix Issue

如何解决问题

The following steps ultimately resolved all of the issues for me. I read above someone mentioning the "chicken and the egg" and it is exactly that concept which led me to this procedure. This explicit process shows that one has to remove any Objective-C code referencing Swift classes until after the header is generated.

下面的步骤最终解决了我所有的问题。我在上面看到有人提到“鸡和蛋”,正是这个概念让我想到了这个过程。这个显式过程表明,在生成头之后,必须删除任何引用Swift类的Objective-C代码。

  1. Comment out the #import "ProductModuleName-Swift.h" statement in your Objective-C implementation file
  2. 注释掉#import“productmodul珐琅- swift”。在Objective-C实现文件中声明。
  3. Comment out any references in the Objective-C implementation file to Swift Classes
  4. 将Objective-C实现文件中的任何引用注释掉到Swift类。
  5. Clean & Build
  6. 清洁和构建
  7. Resolve all errors/warnings
  8. 解决所有的错误/警告
  9. Remove the comment on the #import "ProductModuleName-Swift.h" statement
  10. 删除对#import“productmodul珐琅- swift”的注释。h”声明
  11. Clean & build (successfully or fix any remaining errors, verify that you are not referencing any Swift classes in Objective-C at this point. If so temporarily comment these out)
  12. Clean & build(成功或修复任何剩余的错误,验证您没有在Objective-C中引用任何Swift类)。如果是这样的话,暂时把这些注释掉
  13. Verify that "ProductModuleName-Swift.h" is generated by Cmd-Clicking on the class name of the #import "ProductModuleName-Swift.h" statement
  14. 验证“ProductModuleName-Swift。h“是通过点击#import”productmodul- swift的类名来生成的。h”声明
  15. Remove the comment on the code referencing Swift classes in the Objective-C implementation file.
  16. 删除在Objective-C实现文件中引用Swift类的代码的注释。
  17. Clean & Build as normal (the "ProductModuleName-Swift.h" should be generated and your Objective-C code referencing Swift Classes can be used as normal)
  18. 清洁和构建正常(“产品模块- swift”)。h“应该生成,你的Objective-C代码引用Swift类可以正常使用)

Nota Bene: The answers about changing spaces to underscores and the Defines Module to YES as given above still applies when performing this process, as do the rules specified in the Apple Documentation.

Nota Bene:在执行这一过程时,对于更改空格、下划线和定义模块的答案仍然适用,正如在Apple文档中指定的规则一样。

Bridging Header Path

连接头的路径

In one error, the file ProductModuleName-Bridging-Header.h was not being found during the build process. This fact generated an error

在一个错误中,文件product模块化- bridgingheader。在构建过程中没有发现h。这个事实产生了一个错误。

< unknown>:0: error: bridging header '/Users/Shared/Working/abc/abc-Bridging-Header.h' does not exist

<未知> :0:错误:桥接头/用户/共享/工作/abc/abc- bridgingheader。h”并不存在

Closer inspection of the error indicated that the file would never exist at the location described because it was actually located at (a wrong path)

对错误的仔细检查表明该文件将永远不会存在于所描述的位置,因为它实际上位于(错误的路径)

'/Users/Shared/Working/abc/abc/abc-Bridging-Header.h'. a quick search of the target/projects build settings to make the correction manually and the abc-Swift.h file was again auto generated.

' /用户/共享/工作/ abc / abc / abc-Bridging-Header.h’。快速搜索目标/项目构建设置,以手动和abc-Swift进行修正。h文件再次自动生成。

在Xcode 6中没有创建Objective-C header。

#16


4  

Ok, here are all the things you really need!

好吧,这就是你真正需要的东西!

1.Remove all the swift files you have added, and compile the code, without any errors.

1。删除所有你添加的swift文件,并编译代码,没有任何错误。

----------

在Xcode 6中没有创建Objective-C header。

----------

2.Go to the "Projects" build settings, and set the product module name. Project must have a Product Module Name that does not include spaces.

2。进入“项目”构建设置,并设置产品模块名称。项目必须有一个不包含空格的产品模块名称。

----------

在Xcode 6中没有创建Objective-C header。

----------

3.Defines Module must be set to Yes in Build Settings, under Packaging, in your project, and not target!

3所示。定义模块必须设置为Yes,在构建设置中,在包装下,在您的项目中,而不是目标!

----------

在Xcode 6中没有创建Objective-C header。

----------

4.Now create a swift file or a view controller, in file-> newFile->

4所示。现在创建一个快速的文件或视图控制器,在文件->新文件->。

----------

在Xcode 6中没有创建Objective-C header。

----------

It will ask to create a bridging-header, allow it to make one. If you have declined it once, you will have to manually add a -Bridging-Header.h

它会要求创建一个bridging-header,允许它做一个。如果你已经拒绝了一次,你将不得不手动添加一个- bridging - header。h。

5.Add @objc in the controller, to tell the compiler that there is some swift file, which needs to be exposed to ObjectiveC

5。在控制器中添加@objc,告诉编译器有一些swift文件,需要暴露在ObjectiveC中。

----------

在Xcode 6中没有创建Objective-C header。

----------

6.Build the project and import #import "-Swift.h" in any of the objectiveC controller, and it will work! You can Command-click on it to see the actual file!

6。构建项目并导入#import -Swift。在任何一个目标控制器中,它都能工作!您可以命令点击它来查看实际的文件!

----------

在Xcode 6中没有创建Objective-C header。

----------

Hope this helps!

希望这可以帮助!

#17


3  

You have to import a header in Objective-C classes, which is:

您必须在Objective-C类中导入一个头,这是:

#import “ProductModuleName-Swift.h”

It is automatically generated, on the reference it says "Any Swift files in your target will be visible in Objective-C .m files containing this import statement."

它是自动生成的,在引用中,它说“在Objective-C .m文件中,你的目标中的任何Swift文件都可以看到,包含这个导入语句。”

#18


3  

An actual file in the project is not created ([ProductModuleName]-Swift.h). Cmd + Click on the import either generates it on-the-fly (and in-memory) so you can see how the linkage is done, or opens a file somewhere in some Xcode cache dir, but it's not in the project dir.

项目中的实际文件没有创建([ProductModuleName]-Swift.h)。Cmd +单击导入,可以动态地生成它(和内存中),这样您就可以看到该链接是如何完成的,或者在某个Xcode缓存目录中打开一个文件,但它不在项目目录中。

You need to set Defines Module project prop (in target's Build Settings) to Yes and if your module name has spaces or dashes - use _ in all imports of the [ProductModuleName]-Swift.h file.

您需要将定义模块项目道具(在目标的构建设置中)定义为Yes,如果您的模块名称有空格或dashes,则在所有导入[ProductModuleName]- swift中使用_。h文件。

You can import it in all .h and .m files where you use swift types or you can import it in the .pch.

您可以将它导入到所有的.h和.m文件中,您可以使用swift类型,也可以在.pch中导入它。

So if my Module (project) is named "Test Project", I would import it like this, in the .pch file of my project (just there):

因此,如果我的模块(项目)被命名为“测试项目”,我将会像这样导入它,在我的项目的.pch文件中(就在那里):

#import "Test_Project-Swift.h"

#19


2  

Just a heads up for anyone who used "." in there project name. Xcode will replace the "." with an underscore "_" for the Swift version of the bridging header file. Oddly enough the Bridging-Header.h that is generated does not replace the periods with underscores.

只需要提醒那些使用“。”的人。Xcode将替换“。”,并使用一个下划线“_”来表示桥接头文件的Swift版本。奇怪的是Bridging-Header。生成的h不能用下划线代替句号。

For example a project with the name My.Project would have the following Bridging Header file names.

例如,一个名称为My的项目。项目将有以下桥接头文件名称。

Bridging-Header.h (Autogenerated)

Bridging-Header。h(自动生成)

My.Project-Bridging-Header.h

My.Project-Bridging-Header.h

Swift.h

Swift.h

My_Project.h

My_Project.h

I hope this helps anyone who used a period and was stuck like I was. This file can be found at the following location.

我希望这能帮助任何一个用过一段时间并且像我一样被困住的人。这个文件可以在以下位置找到。

Macintosh HD/Users/user/Library/Developer/Xcode/DerivedData/My.Project-fntdulwpbhbbzdbyrkhanemcrfil/Build/Intermediates/My.Project.build/Debug-iphonesimulator/My.Project.build/DerivedSources

Macintosh HD /用户/用户/图书馆/开发/ Xcode / DerivedData / My.Project-fntdulwpbhbbzdbyrkhanemcrfil /构建/中间体/ My.Project.build Debug-iphonesimulator / My.Project.build / DerivedSources

Take care,

照顾,

Jon

乔恩

#20


2  

Project must have a Module Name not including spaces. Defines Module must be set to Yes in Build Settings, under Packaging. commented out the #import statement:

项目必须有一个模块名称,不包括空格。定义模块必须设置为Yes在构建设置中,在包装下。注释掉了#import语句:

If still you are having error in importing "ProductModuleName-Swift.h" then

如果仍然在导入“productmodul珐琅- swift”时出错。h”然后

//#import "ProductModuleName-Swift.h"

/ / #进口“ProductModuleName-Swift.h”

which revealed a bunch of other errors in my Swift code.

在我的Swift代码中显示了许多其他错误。

Once I fixed these new errors and got the source building successfully, I uncommented out the #import and bingo! The header was created and importing correctly :)

一旦我修复了这些新的错误,并且成功地获得了源构建,我就没有注释掉#import和bingo!标题的创建和导入正确:)

#21


1  

I found a trick that always works on me.

我发现了一个对我有用的技巧。

  1. Create your #import "ProductModuleName-Swift.h" in your appDelegate.h file and in your ProductName-Prefix.pch file. If you don't have it in xcode 6 you can create it with this way Why isn't ProjectName-Prefix.pch created automatically in Xcode 6?
  2. 创建您的# ProductModuleName-Swift进口”。在你的appDelegate h”。h文件和你的产品名称前缀。pch文件。如果你在xcode 6中没有它,你可以用这种方法创建它为什么不是projectname -前缀。pch在Xcode 6中自动创建吗?
  3. Command+shift+k to clean your code, if you receive an error about your "ProductModuleName-Swift.h" delete it from appDelegate.h file.
  4. 命令+shift+k来清理你的代码,如果你收到一个关于你的“产品模块- swift”的错误。从appDelegate中删除它。h文件。
  5. Clean your code again. Now everything will work like a charm
  6. 清洁你的代码了。现在一切都像魔法一样有效。
  7. If you receive again error about the "ProductModuleName-Swift.h", now create again in appDelegate.h file and clean your code again.
  8. 如果您再次收到关于“ProductModuleName-Swift”的错误。现在在appDelegate中再次创建。h文件再次清理你的代码。

Do this work (delete and create the "ProductModuleName-Swift.h" from appDelegate.h file and clean your code) everytime you receive this error to silent it.

完成这项工作(删除并创建“ProductModuleName-Swift”)。从appDelegate h”。h文件和清洁你的代码)每次你收到这个错误的时候保持沉默。

#22


1  

I Found this solution

我发现这个解决方案

  • Create SwiftBridge.h
  • 创建SwiftBridge.h
  • put #import “ProductModuleName-Swift.h”
  • 把#导入“ProductModuleName-Swift.h”
  • Make this .h file public (important) Select the file -> In Show the file Inspector (right bar) -> Make it public
  • 使这个.h文件公开(重要)选择文件->显示文件检查器(右栏)->使其公开。

Now you can

现在你可以

#import "SwiftBridge.h"

instead of ProductModuleName-Swift.h

而不是ProductModuleName-Swift.h

This's a workaround solution, for the next version of Xcode I think this problem will be solved. Good luck

这是一个解决方案,对于Xcode的下一个版本,我认为这个问题将会得到解决。祝你好运

#23


1  

I was having a hard time determining my module name/objective-c's import of swift's headers. I did read a lot of articles here too.

我很难确定我的模块名称/objective-c导入swift的头。我在这里也读过很多文章。

But the definitive answer for your project name with all its included special characters (be it '.' or a numeric or a space) - you can find the text that will work for you in the "Product Module Name" under the target's Build Settings.

但是你的项目名称的最终答案包含了所有的特殊字符(就是它)。或一个数字或空间)-你可以在目标的构建设置下找到为你工作的文本。

For example my target name started with a numeric - "1mg" and the field mentioned above showed "_mg" as my module name.

例如,我的目标名称以数字“1mg”开始,上面提到的字段显示为“_mg”作为我的模块名称。

so I used #import "_mg-Swift.h" and it worked.

所以我使用了#import“_mg-Swift”。h”工作。

在Xcode 6中没有创建Objective-C header。

#24


1  

In my case I had to set the deployment target to at least “OS X 10.9” and the -Swift.h header was automatically generated. Keep in mind that you can get a lot of deprecation warnings when you change the deployment target version, especially when you have an older and very large Objective C code base. In our case we also had a lot of work to do in XIB files & view classes.

在我的情况下,我必须将部署目标设置为至少“OS X 10.9”和-Swift。h头自动生成。请记住,当您更改部署目标版本时,您可以获得大量的弃用警告,特别是当您有一个更老的、非常大的Objective C代码库时。在我们的案例中,我们在XIB文件和视图类中也有很多工作要做。

#25


0  

If you were able to build a project before, with no issues related to “ProductModuleName-Swift.h” not found error, and now you are getting that nasty errors again, the reason might sit in your recent changes.

如果您能够在之前构建一个项目,没有与“ProductModuleName-Swift”相关的问题。“没有发现错误,现在你又犯了严重的错误,原因可能是你最近的变化。”

For me this was by (accidental) incorrect .swift file encoding. Reverting changes and bringing the back manually, does the job.

对我来说,这是(偶然)错误的。swift文件编码。恢复更改并手动恢复,完成任务。

#26


0  

This may be an obvious point (maybe too obvious), but you must have at least one swift file in the project for the header to generate. If you are writing boilerplate or config code with the intention of writing swift later the import won't work.

这可能是一个明显的问题(可能太明显了),但是您必须在项目中至少有一个swift文件来生成。如果您正在编写样板文件或配置代码,并打算在以后快速写入,那么导入将不会起作用。

#27


0  

I had to delete WatchOS2 swift code from my Objective C project. And only after that XCode offered to generate -Swift.h

我不得不从我的Objective C项目中删除WatchOS2 swift代码。而且只有在XCode提出要生成-Swift.h之后。

#28


0  

I had similar problem but my project was compiling before and suddenly got error after few files code change. It took me while to figure out why I am getting 'File not found' error for myproject-swift.h file. The code changes I had done had some errors. Xcode did not point put those error instead all time showing the 'File not found error'. Then got copy of previous version code and I compared with new code and merged file one by one. After each file merge complied the project to find the error. So bottom line is if you have error in your code Xcode may just display 'file not found error' for myproject-swift.h file. Most likely you have compilation error in your project. Clean those error and it will work.

我遇到过类似的问题,但我的项目是之前编译的,在很少的文件代码更改后突然出现错误。我花了一段时间才弄明白为什么我的“文件没有被发现”错误的myproject-swift。h文件。我所做的代码更改有一些错误。Xcode没有指出这些错误,而是一直显示“文件未发现错误”。然后得到了以前版本代码的副本,我将新代码和合并后的文件逐一进行了比较。在每个文件合并后,执行项目以查找错误。所以底线是,如果你的代码Xcode中有错误,那么就可以显示myprojectswift的“文件未发现错误”。h文件。很可能在您的项目中有编译错误。清除这些错误,它就会起作用。

#29


0  

If you're using something like Cocoapods (and working out of the workspace rather than the project) try opening the project and building it before opening the workspace and building. YMMV.

如果您正在使用像Cocoapods(并在工作空间而不是项目中工作)之类的东西,尝试打开项目并在打开工作区和构建之前构建它。YMMV。

#30


0  

Sometimes you just need to unset and then set again the target membership on the obj-c .m file.

有时您只需要取消设置,然后再次设置objc .m文件的目标成员。

#1


152  

Now it works.

现在它的工作原理。

  1. Project must have a Product Module Name that does not include spaces.
  2. 项目必须有一个不包含空格的产品模块名称。
  3. Defines Module must be set to Yes in Build Settings, under Packaging.
  4. 定义模块必须设置为Yes在构建设置中,在包装下。

Finally works. Thanks to everyone for the help :-)

最后的作品。感谢大家的帮助:-)

#2


121  

I had a similar problem and found that you can only add

我有一个类似的问题,发现你只能添加。

#import “ProductModuleName-Swift.h”

#进口“ProductModuleName-Swift.h”

to obj-c .m files, not .h files for the umbrella header to be found

要找到objc .m文件,而不是。h文件。

#3


52  

I found that I had to fix all build errors before it would generate the file.

我发现,在生成文件之前,我必须修复所有的构建错误。

The problem for me was that it was a chicken/egg problem, in that I didn't see any build errors until I'd actually commented out the #import statement:

我的问题是,这是一个鸡/蛋的问题,因为我没有看到任何构建错误,直到我实际上注释掉了#import语句:

//#import "ProductModuleName-Swift.h"

/ / #进口“ProductModuleName-Swift.h”

which revealed a bunch of other errors in my Swift code.

在我的Swift代码中显示了许多其他错误。

Once I fixed these new errors and got the source building successfully, I uncommented out the #import and bingo! The header was created and importing correctly :)

一旦我修复了这些新的错误,并且成功地获得了源构建,我就没有注释掉#import和bingo!标题的创建和导入正确:)

#4


41  

If your project module name has spaces in it, you must replace the spaces with an underscore.

如果项目模块名称中有空格,则必须用下划线替换空格。

For instance, if your project name is "My Project", you would use:

例如,如果您的项目名称是“我的项目”,您将使用:

#import "My_Project-Swift.h"

#进口“My_Project-Swift.h”

#5


39  

If you're like me you've probably got the header name wrong. After bashing my head for a while I looked for the file in DerivedData and sure enough it's there. On my setup (using the standard derived data folder, I believe):

如果你像我一样,你可能把标题写错了。在敲打我的脑袋一段时间后,我寻找文件,并确定它在那里。在我的设置(使用标准的派生数据文件夹,我相信):

cd ~/Library/Developer/Xcode/DerivedData
find * -iname '*Swift.h'

Will find it. If nothing in that folder matches then Xcode is not generating it.

会找到它的。如果该文件夹中没有匹配,那么Xcode就不会生成它。

I'm using Xcode Version 6.2 (6C86e)

我使用的是Xcode 6.2 (6C86e)

#6


30  

* The only important thing is: *

唯一重要的是:*。

to use the defined "Product Module Name" in the target, followed by -Swift.h

在目标中使用定义的“产品模块名称”,然后是-Swift.h。

#import <Product Module Name>-Swift.h

// in each ObjectiveC .m file having to use swift classes
// no matter in which swift files these classes sit.

No matter if "Defines Module" param is set to Yes or No or if "Product Module Name" Project is not set.

无论“定义模块”param是否设置为Yes或No,或“产品模块名称”项目未设置。

Reminder: Swift classes must deriving from NSObject or been tagged with @objc attribute in order to be exposed to ObjectiveC / Foundation || Cocoa ...

提示:Swift类必须从NSObject派生或被标记为@objc属性,以便暴露于ObjectiveC / Foundation || Cocoa…

#7


23  

I wanted to add one more reason you might find an issue with this - I was creating a framework that mixed Swift and Objective-C code. I was not able to import the Swift classes outside the framework - I checked for the -Swift.h file and it was being generated but was empty.

我想增加一个你可能会发现这个问题的原因——我正在创建一个框架,它混合了Swift和Objective-C代码。我无法在框架之外导入Swift类——我检查了-Swift。文件正在生成,但是是空的。

The problem turned out to be very, very simple - I had not declared any of my Swift classes public! As soon as I added the public keyword to the classes, I was able to use them from classes inside and outside the framework.

问题变得非常非常简单——我没有公开宣布我的任何一个快速课程。只要我将公共关键字添加到类中,我就能够在框架内和外部的类中使用它们。

Also of note, inside the framework (inside .m files only as another answer mentions) I had to import the -Swift.h file as:

另外,在框架内(在.m文件中只有另一个答案提到),我必须导入-Swift。h文件:

#import <FrameworkName/FrameworkName-Swift.h>

#8


13  

I had the same problem. Seems like you have to adjust the settings (Defines Module and Product Module Name) before you add your first Swift file.

我遇到了同样的问题。在添加第一个Swift文件之前,似乎必须调整设置(定义模块和产品模块名称)。

If you do it afterwards the "*-Swift.h" file will not be generated for this project even if you add further Swift files or delete the Swift file and create a new one.

如果你事后做,“*-Swift”。h“文件将不会为这个项目生成,即使你添加了更多的Swift文件或删除Swift文件并创建一个新的文件。

#9


12  

Here is another variation of the moduleName-Swift.h not being generated.

这是另一个变化的模块化swift。h不生成。

I decided to include IOS Charts in my project but did not want to mingle the sources in the same directory, so I placed the Charts Project folder next to my code's project folder. I dragged the Charts project into my Project's Navigator Bar and included the framework in the my project target's Embedded Binaries list in the General project settings and set the Embedded Content Contains Swift Code switch to yes in my project's Build Settings tab in the Build Options section.

我决定在我的项目中加入IOS图表,但不想在同一个目录中混合源代码,所以我将图表项目文件夹放在代码的项目文件夹旁边。我将图表项目拖到项目的导航栏中,并在项目目标的一般项目设置中包含了项目目标的嵌入式二进制列表中的框架,并设置嵌入的内容,其中包含了在构建选项部分中我的项目的Build settings选项卡中的Swift代码切换。

My project's moduleName-Swift.h file would never generate no matter what other switches or settings suggested here. Finally, using Lou Z's method of seeking out the -Swift.h files, I saw that a Charts-Swift.h file was being generated deep in my project's xcode Build directory in Charts.framework/Headers/

我的项目是moduleName-Swift。h文件无论在这里建议什么其他开关或设置,都不会生成。最后,运用卢茨的方法寻找-斯威夫特。h文件,我看到了一个图表。h文件正在我的项目的xcode构建目录中生成。

The solution to using Daniel Gindi's ios-charts Swift package without including the code in my project's source directory was to add:

在我的项目的源代码目录中,使用Daniel Gindi的ios-charts Swift包的解决方案是:

#import "Charts/Charts-Swift.h"

To the modules charting my project's data.

到模块中绘制我的项目的数据。

#10


12  

The file name is always preceded by your Target name. It is referred as Product name but practically it is the target name. So if you want it to build for a new target be ready to expect that_target-Swift.h file.

文件名称总是在您的目标名称前面。它被称为产品名称,但实际上它是目标名称。因此,如果你想让它为一个新目标而构建,那么就准备好期待这个目标- swift。h文件。

One way to handle this is

一种处理方法是。

  1. Add a preprocessor for each of your target that is the name of your target itself (without spaces). Ex. MY_TARGET=1. Add this in Project settings->Build Settings->Preprocessor Macros for each of your targets.
  2. 为每个目标添加一个预处理器,这是目标本身的名称(没有空格)。MY_TARGET = 1。在项目设置中添加这一点->构建设置->预处理器宏为每个目标。
  3. If you are using a PCH file,
  4. 如果你使用的是PCH文件,

Add these lines in the PCH file

在PCH文件中添加这些行。

#if MY_TARGET==1
#include "My_Target-Swift.h"
#elif THAT_TARGET==1
#include "That_Target-Swift.h" 
#endif

Advantage of using PCH file is that you don't have to include the headers everywhere.

使用PCH文件的优点是不需要在任何地方都包含头文件。

  1. If you are not using a PCH file, just add these same lines in a single header and include that header wherever you need to use the swift classes.
  2. 如果您没有使用PCH文件,只需在单个标头中添加这些相同的行,并在需要使用swift类的任何地方都包含该标题。

This should work just fine.

这应该没问题。

#11


10  

If XCode is actually generating your -Swift.h header (deep inside DerivedData) but it doesn't refer to your Swift classes, make sure you also have a bridging header defined. The way I read the docs implied I only needed that for calling Objective-C from Swift, but it seems to be necessary for calling Swift from Objective-C too.

如果XCode实际上生成了你的-Swift。h标头(deep inside DerivedData),但它不涉及您的Swift类,确保您也定义了一个桥接头。我阅读文档的方式暗示了我只需要从Swift调用Objective-C,但是从Objective-C中调用Swift似乎是必要的。

See my answer: https://*.com/a/27972946/337392

看到我的答案是:https://*.com/a/27972946/337392

EDIT: It is because of public vs. internal access modifiers, as I eventually found explained in the Apple docs:-

编辑:这是因为公共和内部访问修饰符,我最终在苹果文档中找到了解释:-。

By default, the generated header contains interfaces for Swift declarations marked with the public modifier. It also contains those marked with the internal modifier if your app target has an Objective-C bridging header.

默认情况下,生成的头包含与公共修饰符标记的Swift声明的接口。如果你的应用目标有一个Objective-C桥接头,它也会包含那些带有内部修饰符的标记。

#12


9  

Allow me to share my experiences trying to use Swift in an old objc project. I did not have to set Defines module to YES.

请允许我分享我在旧的objc项目中使用Swift的经验。我不需要设置定义模块为YES。

In my case I needed to manually make sure there was an objc Bridging Header. Only the generated interface header name was present in my build settings.

在我的例子中,我需要手动确保有一个objc连接头。在我的构建设置中只存在生成的接口标题名称。

在Xcode 6中没有创建Objective-C header。

This lead to a MyApp-Swift.h file to being generated, but without any traces of my Swift classes.

这就导致了MyApp-Swift。h文件生成,但是没有我的Swift类的任何痕迹。

The Apple documentation says that you will be prompted to create a bridging header when adding your first swift file. Well, I wasn't. I manually added a MyApp-Bridging-header.h file and pointed to it in the "Objective-C Bridging Header" field. That made my MyApp-Swift.h file become populated with my Swift classes.

苹果文档说,在添加第一个swift文件时,将提示您创建一个桥接头。好吧,我不是。我手动添加了一个myapp - bridgingheader。h文件并在“Objective-C桥接头”字段中指向它。这使我MyApp-Swift。h文件用我的Swift类填充。

Docs: Importing Swift into Objective-C

文档:将Swift导入Objective-C。

#13


6  

Seconding what a lot of people have here, but adding a pertinent screen shot. Swift and Obj-C code can certainly live together. It's not an all or none game.

这里有很多人的支持,但是增加了一个相关的屏幕截图。Swift和objc代码可以共存。这不是一场全部或一场比赛。

To access Swift files in your Objective-C, all you need to do is add this call to your Obj-C file (in the .m / implementation file):

要在Objective-C中访问Swift文件,需要做的就是将这个调用添加到objc - c文件中(在.m /实现文件中):

#import "{product_module_name}-Swift.h"

(Where {product_module_name} represents the product module name of your project). Rather than try to guess your product module name or figure out corner cases with spaces and special characters, just go to the build settings tab in the project and type in "product module name" - the inspector will reveal yours to you. Mine was something I did not expect it to be. Check out this screen shot if you're confused.

({product_module_name}表示项目的产品模块名称)。与其尝试猜测您的产品模块名称或计算出带有空格和特殊字符的角落案例,不如直接进入项目的build settings选项卡并输入“产品模块名称”——检查器将向您显示您的产品模块名称。我的想法是我没有预料到的。如果你感到困惑,看看这个屏幕截图。

在Xcode 6中没有创建Objective-C header。

And to get Obj-c code working in Swift, you just need to add a bridging header file and import the relevant Obj-C headers there.

为了让objc -c代码在Swift中工作,您只需添加一个桥接头文件并导入相关的object -c header。

#14


4  

The most important thing is that This file is invisible!!! At least it is in Xcode6 beta5. There will be no such file named "YourModule-Swift.h" in your workspace. Just make sure you have module name and defines module set to yes, and use it in your Objective-C class.

最重要的是这个文件是看不见的!!!至少在Xcode6 beta5。将不会有这样的文件命名为“YourModule-Swift”。h”在你的工作区。只需确保您拥有模块名称并将模块集定义为yes,并在Objective-C类中使用它。

#15


4  

This answer addresses the use-case where you may already have some Objective-C code that calls Swift classes and then you start receiving this error.

这个答案解决了用例,您可能已经有一些Objective-C代码调用Swift类,然后您开始接收这个错误。

How To Fix Issue

如何解决问题

The following steps ultimately resolved all of the issues for me. I read above someone mentioning the "chicken and the egg" and it is exactly that concept which led me to this procedure. This explicit process shows that one has to remove any Objective-C code referencing Swift classes until after the header is generated.

下面的步骤最终解决了我所有的问题。我在上面看到有人提到“鸡和蛋”,正是这个概念让我想到了这个过程。这个显式过程表明,在生成头之后,必须删除任何引用Swift类的Objective-C代码。

  1. Comment out the #import "ProductModuleName-Swift.h" statement in your Objective-C implementation file
  2. 注释掉#import“productmodul珐琅- swift”。在Objective-C实现文件中声明。
  3. Comment out any references in the Objective-C implementation file to Swift Classes
  4. 将Objective-C实现文件中的任何引用注释掉到Swift类。
  5. Clean & Build
  6. 清洁和构建
  7. Resolve all errors/warnings
  8. 解决所有的错误/警告
  9. Remove the comment on the #import "ProductModuleName-Swift.h" statement
  10. 删除对#import“productmodul珐琅- swift”的注释。h”声明
  11. Clean & build (successfully or fix any remaining errors, verify that you are not referencing any Swift classes in Objective-C at this point. If so temporarily comment these out)
  12. Clean & build(成功或修复任何剩余的错误,验证您没有在Objective-C中引用任何Swift类)。如果是这样的话,暂时把这些注释掉
  13. Verify that "ProductModuleName-Swift.h" is generated by Cmd-Clicking on the class name of the #import "ProductModuleName-Swift.h" statement
  14. 验证“ProductModuleName-Swift。h“是通过点击#import”productmodul- swift的类名来生成的。h”声明
  15. Remove the comment on the code referencing Swift classes in the Objective-C implementation file.
  16. 删除在Objective-C实现文件中引用Swift类的代码的注释。
  17. Clean & Build as normal (the "ProductModuleName-Swift.h" should be generated and your Objective-C code referencing Swift Classes can be used as normal)
  18. 清洁和构建正常(“产品模块- swift”)。h“应该生成,你的Objective-C代码引用Swift类可以正常使用)

Nota Bene: The answers about changing spaces to underscores and the Defines Module to YES as given above still applies when performing this process, as do the rules specified in the Apple Documentation.

Nota Bene:在执行这一过程时,对于更改空格、下划线和定义模块的答案仍然适用,正如在Apple文档中指定的规则一样。

Bridging Header Path

连接头的路径

In one error, the file ProductModuleName-Bridging-Header.h was not being found during the build process. This fact generated an error

在一个错误中,文件product模块化- bridgingheader。在构建过程中没有发现h。这个事实产生了一个错误。

< unknown>:0: error: bridging header '/Users/Shared/Working/abc/abc-Bridging-Header.h' does not exist

<未知> :0:错误:桥接头/用户/共享/工作/abc/abc- bridgingheader。h”并不存在

Closer inspection of the error indicated that the file would never exist at the location described because it was actually located at (a wrong path)

对错误的仔细检查表明该文件将永远不会存在于所描述的位置,因为它实际上位于(错误的路径)

'/Users/Shared/Working/abc/abc/abc-Bridging-Header.h'. a quick search of the target/projects build settings to make the correction manually and the abc-Swift.h file was again auto generated.

' /用户/共享/工作/ abc / abc / abc-Bridging-Header.h’。快速搜索目标/项目构建设置,以手动和abc-Swift进行修正。h文件再次自动生成。

在Xcode 6中没有创建Objective-C header。

#16


4  

Ok, here are all the things you really need!

好吧,这就是你真正需要的东西!

1.Remove all the swift files you have added, and compile the code, without any errors.

1。删除所有你添加的swift文件,并编译代码,没有任何错误。

----------

在Xcode 6中没有创建Objective-C header。

----------

2.Go to the "Projects" build settings, and set the product module name. Project must have a Product Module Name that does not include spaces.

2。进入“项目”构建设置,并设置产品模块名称。项目必须有一个不包含空格的产品模块名称。

----------

在Xcode 6中没有创建Objective-C header。

----------

3.Defines Module must be set to Yes in Build Settings, under Packaging, in your project, and not target!

3所示。定义模块必须设置为Yes,在构建设置中,在包装下,在您的项目中,而不是目标!

----------

在Xcode 6中没有创建Objective-C header。

----------

4.Now create a swift file or a view controller, in file-> newFile->

4所示。现在创建一个快速的文件或视图控制器,在文件->新文件->。

----------

在Xcode 6中没有创建Objective-C header。

----------

It will ask to create a bridging-header, allow it to make one. If you have declined it once, you will have to manually add a -Bridging-Header.h

它会要求创建一个bridging-header,允许它做一个。如果你已经拒绝了一次,你将不得不手动添加一个- bridging - header。h。

5.Add @objc in the controller, to tell the compiler that there is some swift file, which needs to be exposed to ObjectiveC

5。在控制器中添加@objc,告诉编译器有一些swift文件,需要暴露在ObjectiveC中。

----------

在Xcode 6中没有创建Objective-C header。

----------

6.Build the project and import #import "-Swift.h" in any of the objectiveC controller, and it will work! You can Command-click on it to see the actual file!

6。构建项目并导入#import -Swift。在任何一个目标控制器中,它都能工作!您可以命令点击它来查看实际的文件!

----------

在Xcode 6中没有创建Objective-C header。

----------

Hope this helps!

希望这可以帮助!

#17


3  

You have to import a header in Objective-C classes, which is:

您必须在Objective-C类中导入一个头,这是:

#import “ProductModuleName-Swift.h”

It is automatically generated, on the reference it says "Any Swift files in your target will be visible in Objective-C .m files containing this import statement."

它是自动生成的,在引用中,它说“在Objective-C .m文件中,你的目标中的任何Swift文件都可以看到,包含这个导入语句。”

#18


3  

An actual file in the project is not created ([ProductModuleName]-Swift.h). Cmd + Click on the import either generates it on-the-fly (and in-memory) so you can see how the linkage is done, or opens a file somewhere in some Xcode cache dir, but it's not in the project dir.

项目中的实际文件没有创建([ProductModuleName]-Swift.h)。Cmd +单击导入,可以动态地生成它(和内存中),这样您就可以看到该链接是如何完成的,或者在某个Xcode缓存目录中打开一个文件,但它不在项目目录中。

You need to set Defines Module project prop (in target's Build Settings) to Yes and if your module name has spaces or dashes - use _ in all imports of the [ProductModuleName]-Swift.h file.

您需要将定义模块项目道具(在目标的构建设置中)定义为Yes,如果您的模块名称有空格或dashes,则在所有导入[ProductModuleName]- swift中使用_。h文件。

You can import it in all .h and .m files where you use swift types or you can import it in the .pch.

您可以将它导入到所有的.h和.m文件中,您可以使用swift类型,也可以在.pch中导入它。

So if my Module (project) is named "Test Project", I would import it like this, in the .pch file of my project (just there):

因此,如果我的模块(项目)被命名为“测试项目”,我将会像这样导入它,在我的项目的.pch文件中(就在那里):

#import "Test_Project-Swift.h"

#19


2  

Just a heads up for anyone who used "." in there project name. Xcode will replace the "." with an underscore "_" for the Swift version of the bridging header file. Oddly enough the Bridging-Header.h that is generated does not replace the periods with underscores.

只需要提醒那些使用“。”的人。Xcode将替换“。”,并使用一个下划线“_”来表示桥接头文件的Swift版本。奇怪的是Bridging-Header。生成的h不能用下划线代替句号。

For example a project with the name My.Project would have the following Bridging Header file names.

例如,一个名称为My的项目。项目将有以下桥接头文件名称。

Bridging-Header.h (Autogenerated)

Bridging-Header。h(自动生成)

My.Project-Bridging-Header.h

My.Project-Bridging-Header.h

Swift.h

Swift.h

My_Project.h

My_Project.h

I hope this helps anyone who used a period and was stuck like I was. This file can be found at the following location.

我希望这能帮助任何一个用过一段时间并且像我一样被困住的人。这个文件可以在以下位置找到。

Macintosh HD/Users/user/Library/Developer/Xcode/DerivedData/My.Project-fntdulwpbhbbzdbyrkhanemcrfil/Build/Intermediates/My.Project.build/Debug-iphonesimulator/My.Project.build/DerivedSources

Macintosh HD /用户/用户/图书馆/开发/ Xcode / DerivedData / My.Project-fntdulwpbhbbzdbyrkhanemcrfil /构建/中间体/ My.Project.build Debug-iphonesimulator / My.Project.build / DerivedSources

Take care,

照顾,

Jon

乔恩

#20


2  

Project must have a Module Name not including spaces. Defines Module must be set to Yes in Build Settings, under Packaging. commented out the #import statement:

项目必须有一个模块名称,不包括空格。定义模块必须设置为Yes在构建设置中,在包装下。注释掉了#import语句:

If still you are having error in importing "ProductModuleName-Swift.h" then

如果仍然在导入“productmodul珐琅- swift”时出错。h”然后

//#import "ProductModuleName-Swift.h"

/ / #进口“ProductModuleName-Swift.h”

which revealed a bunch of other errors in my Swift code.

在我的Swift代码中显示了许多其他错误。

Once I fixed these new errors and got the source building successfully, I uncommented out the #import and bingo! The header was created and importing correctly :)

一旦我修复了这些新的错误,并且成功地获得了源构建,我就没有注释掉#import和bingo!标题的创建和导入正确:)

#21


1  

I found a trick that always works on me.

我发现了一个对我有用的技巧。

  1. Create your #import "ProductModuleName-Swift.h" in your appDelegate.h file and in your ProductName-Prefix.pch file. If you don't have it in xcode 6 you can create it with this way Why isn't ProjectName-Prefix.pch created automatically in Xcode 6?
  2. 创建您的# ProductModuleName-Swift进口”。在你的appDelegate h”。h文件和你的产品名称前缀。pch文件。如果你在xcode 6中没有它,你可以用这种方法创建它为什么不是projectname -前缀。pch在Xcode 6中自动创建吗?
  3. Command+shift+k to clean your code, if you receive an error about your "ProductModuleName-Swift.h" delete it from appDelegate.h file.
  4. 命令+shift+k来清理你的代码,如果你收到一个关于你的“产品模块- swift”的错误。从appDelegate中删除它。h文件。
  5. Clean your code again. Now everything will work like a charm
  6. 清洁你的代码了。现在一切都像魔法一样有效。
  7. If you receive again error about the "ProductModuleName-Swift.h", now create again in appDelegate.h file and clean your code again.
  8. 如果您再次收到关于“ProductModuleName-Swift”的错误。现在在appDelegate中再次创建。h文件再次清理你的代码。

Do this work (delete and create the "ProductModuleName-Swift.h" from appDelegate.h file and clean your code) everytime you receive this error to silent it.

完成这项工作(删除并创建“ProductModuleName-Swift”)。从appDelegate h”。h文件和清洁你的代码)每次你收到这个错误的时候保持沉默。

#22


1  

I Found this solution

我发现这个解决方案

  • Create SwiftBridge.h
  • 创建SwiftBridge.h
  • put #import “ProductModuleName-Swift.h”
  • 把#导入“ProductModuleName-Swift.h”
  • Make this .h file public (important) Select the file -> In Show the file Inspector (right bar) -> Make it public
  • 使这个.h文件公开(重要)选择文件->显示文件检查器(右栏)->使其公开。

Now you can

现在你可以

#import "SwiftBridge.h"

instead of ProductModuleName-Swift.h

而不是ProductModuleName-Swift.h

This's a workaround solution, for the next version of Xcode I think this problem will be solved. Good luck

这是一个解决方案,对于Xcode的下一个版本,我认为这个问题将会得到解决。祝你好运

#23


1  

I was having a hard time determining my module name/objective-c's import of swift's headers. I did read a lot of articles here too.

我很难确定我的模块名称/objective-c导入swift的头。我在这里也读过很多文章。

But the definitive answer for your project name with all its included special characters (be it '.' or a numeric or a space) - you can find the text that will work for you in the "Product Module Name" under the target's Build Settings.

但是你的项目名称的最终答案包含了所有的特殊字符(就是它)。或一个数字或空间)-你可以在目标的构建设置下找到为你工作的文本。

For example my target name started with a numeric - "1mg" and the field mentioned above showed "_mg" as my module name.

例如,我的目标名称以数字“1mg”开始,上面提到的字段显示为“_mg”作为我的模块名称。

so I used #import "_mg-Swift.h" and it worked.

所以我使用了#import“_mg-Swift”。h”工作。

在Xcode 6中没有创建Objective-C header。

#24


1  

In my case I had to set the deployment target to at least “OS X 10.9” and the -Swift.h header was automatically generated. Keep in mind that you can get a lot of deprecation warnings when you change the deployment target version, especially when you have an older and very large Objective C code base. In our case we also had a lot of work to do in XIB files & view classes.

在我的情况下,我必须将部署目标设置为至少“OS X 10.9”和-Swift。h头自动生成。请记住,当您更改部署目标版本时,您可以获得大量的弃用警告,特别是当您有一个更老的、非常大的Objective C代码库时。在我们的案例中,我们在XIB文件和视图类中也有很多工作要做。

#25


0  

If you were able to build a project before, with no issues related to “ProductModuleName-Swift.h” not found error, and now you are getting that nasty errors again, the reason might sit in your recent changes.

如果您能够在之前构建一个项目,没有与“ProductModuleName-Swift”相关的问题。“没有发现错误,现在你又犯了严重的错误,原因可能是你最近的变化。”

For me this was by (accidental) incorrect .swift file encoding. Reverting changes and bringing the back manually, does the job.

对我来说,这是(偶然)错误的。swift文件编码。恢复更改并手动恢复,完成任务。

#26


0  

This may be an obvious point (maybe too obvious), but you must have at least one swift file in the project for the header to generate. If you are writing boilerplate or config code with the intention of writing swift later the import won't work.

这可能是一个明显的问题(可能太明显了),但是您必须在项目中至少有一个swift文件来生成。如果您正在编写样板文件或配置代码,并打算在以后快速写入,那么导入将不会起作用。

#27


0  

I had to delete WatchOS2 swift code from my Objective C project. And only after that XCode offered to generate -Swift.h

我不得不从我的Objective C项目中删除WatchOS2 swift代码。而且只有在XCode提出要生成-Swift.h之后。

#28


0  

I had similar problem but my project was compiling before and suddenly got error after few files code change. It took me while to figure out why I am getting 'File not found' error for myproject-swift.h file. The code changes I had done had some errors. Xcode did not point put those error instead all time showing the 'File not found error'. Then got copy of previous version code and I compared with new code and merged file one by one. After each file merge complied the project to find the error. So bottom line is if you have error in your code Xcode may just display 'file not found error' for myproject-swift.h file. Most likely you have compilation error in your project. Clean those error and it will work.

我遇到过类似的问题,但我的项目是之前编译的,在很少的文件代码更改后突然出现错误。我花了一段时间才弄明白为什么我的“文件没有被发现”错误的myproject-swift。h文件。我所做的代码更改有一些错误。Xcode没有指出这些错误,而是一直显示“文件未发现错误”。然后得到了以前版本代码的副本,我将新代码和合并后的文件逐一进行了比较。在每个文件合并后,执行项目以查找错误。所以底线是,如果你的代码Xcode中有错误,那么就可以显示myprojectswift的“文件未发现错误”。h文件。很可能在您的项目中有编译错误。清除这些错误,它就会起作用。

#29


0  

If you're using something like Cocoapods (and working out of the workspace rather than the project) try opening the project and building it before opening the workspace and building. YMMV.

如果您正在使用像Cocoapods(并在工作空间而不是项目中工作)之类的东西,尝试打开项目并在打开工作区和构建之前构建它。YMMV。

#30


0  

Sometimes you just need to unset and then set again the target membership on the obj-c .m file.

有时您只需要取消设置,然后再次设置objc .m文件的目标成员。