未在 Xcode 6 中创建的 Swift 到 Objective-C 标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24062618/
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
Swift to Objective-C header not created in Xcode 6
提问by David Kristensen
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.hheader file that is supposed to expose Swift classes either marked @objcor subclasses of ObjC classes, is nowhereto be found :-(
但是,*-Swift.h是应该暴露斯威夫特类或者标头文件@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 Objective-C).
在我的主应用程序代码(仍然是 Objective-C)中,我没有看到任何关于如何完成使用 Swift 编写的新子类的具体说明。
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 行),所以一次转换它是不可能的。
回答by David Kristensen
Now it works.
现在它起作用了。
- Project must have a Product Module Name that does notinclude spaces.
- Defines Modulemust be set to Yesin Build Settings, under Packaging.
- 项目必须具有不包含空格的产品模块名称。
- 必须在“打包”下的“构建设置”中将“定义模块”设置为“是”。
Finally works. Thanks to everyone for the help :-)
终于工作了。感谢大家的帮助:-)
回答by macduff
I had a similar problem and found that you can only add
我有一个类似的问题,发现你只能添加
#import "ProductModuleName-Swift.h"
#import "ProductModuleName-Swift.h"
to obj-c .m files, not .h files for the umbrella header to be found
到 obj-c .m 文件,而不是要找到的伞头文件的 .h 文件
回答by petehare
I found that I had to fix allbuild 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 #importstatement:
我的问题是这是一个鸡/蛋问题,因为在我实际注释掉该#import语句之前我没有看到任何构建错误:
//#import "ProductModuleName-Swift.h"
//#import "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 #importand bingo! The header was created and importing correctly :)
一旦我修复了这些新错误并成功构建了源代码,我就取消了注释#import和宾果游戏!标题已创建并正确导入:)
回答by Lou Zell
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):
如果你像我一样,你可能把标题名称弄错了。在我猛烈抨击了一段时间后,我在 DerivedData 中查找了该文件,果然它就在那里。在我的设置中(我相信使用标准派生数据文件夹):
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)
回答by JohnMorrison
If your project module name has spaces in it, you mustreplace the spaces with an underscore.
如果您的项目模块名称中包含空格,则必须用下划线替换空格。
For instance, if your project name is "My Project", you would use:
例如,如果您的项目名称是“我的项目”,您将使用:
#import "My_Project-Swift.h"
#import "My_Project-Swift.h"
回答by Luc-Olivier
* 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.
无论“定义模块”参数是否设置为是或否,或者是否未设置“产品模块名称”项目。
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 || 可可 ...
回答by Kendall Helmstetter Gelner
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.h 文件,它正在生成但为空。
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.
结果证明这个问题非常非常简单——我没有将我的任何 Swift 类声明为公开的!一旦我将 public 关键字添加到类中,我就可以在框架内部和外部的类中使用它们。
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>
回答by user3540830
I had the same problem. Seems like you have to adjust the settings (Defines Module and Product Module Name) beforeyou 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 文件或删除 Swift 文件并创建一个新文件,也不会为此项目生成“*-Swift.h”文件。
回答by Nicolai Dahl
Allow me to share my experiences trying to use Swift in an old objc project. I did not have to set Defines moduleto YES.
请允许我分享我在旧的 objc 项目中尝试使用 Swift 的经验。我不必设置Defines module为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 桥接头。我的构建设置中只存在生成的接口标头名称。
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.hfile and pointed to it in the "Objective-C Bridging Header" field. That made my MyApp-Swift.h file become populated with my Swift classes.
Apple 文档说,在添加第一个 swift 文件时,系统会提示您创建桥接头。嗯,我不是。我手动添加了一个MyApp-Bridging-header.h文件并在“Objective-C Bridging Header”字段中指向它。这使得我的 MyApp-Swift.h 文件被我的 Swift 类填充。
回答by gjpc
Here is another variation of the moduleName-Swift.h not being generated.
这是未生成的 moduleName-Swift.h 的另一个变体。
I decided to include IOS Chartsin 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 Binarieslist in the General project settings and set the Embedded Content Contains Swift Codeswitch to yes in my project's Build Settingstab in the Build Optionssection.
我决定在我的项目中包含IOS Charts,但不想将源代码混合在同一目录中,所以我将 Charts Project 文件夹放在我的代码项目文件夹旁边。我将 Charts 项目拖到我的项目的导航栏中,并将框架包含在我的项目目标的嵌入式二进制文件列表中的常规项目设置中,并在我的项目的构建选项部分的构建设置选项卡中将嵌入式内容包含 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 文件都不会生成。最后,使用 Lou Z 的查找 -Swift.h 文件的方法,我看到在我的项目的 xcode Build 目录深处的 Charts.framework/Headers/ 中生成了一个 Charts-Swift.h 文件
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.
到绘制我的项目数据的模块。


