我最近一直在努力将Swift添加到一个现有的项目中,以便在现实世界中进行尝试。

在向项目中添加Swift源文件时,我对获得“桥接头”没有任何问题,也就是说,Objective-C到Swift。

但是*-Swift.h头文件应该公开Swift类,无论是标记为@objc的类还是ObjC类的子类,都找不到:-(

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

我是首席开发人员的应用程序有一个相当大的代码库(70.000行),所以一次性过渡它是不可能的。


当前回答

我找到了这个解决方案

创建SwiftBridge.h 导入" ProductModuleName-Swift.h " 将此.h文件公开(重要)选择该文件->在“显示文件检查器”中(右栏)->将其公开

现在你可以

#import "SwiftBridge.h"

而不是ProductModuleName-Swift.h

这是一个变通的解决方案,在下一个版本的Xcode中,我认为这个问题会得到解决。 祝你好运

其他回答

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.

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

问题变得非常非常简单——我没有将我的任何Swift类声明为public!一旦我将public关键字添加到类中,我就可以从框架内外的类中使用它们了。

同样值得注意的是,在框架内(只有在。m文件内,另一个答案提到),我必须导入-Swift.h文件:

#import <FrameworkName/FrameworkName-Swift.h>

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

#进口“ProductModuleName-Swift.h”

到obj-c .m文件,而不是找到伞头的.h文件

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

如何解决问题

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

Comment out the #import "ProductModuleName-Swift.h" statement in your Objective-C implementation file Comment out any references in the Objective-C implementation file to Swift Classes Clean & Build Resolve all errors/warnings Remove the comment on the #import "ProductModuleName-Swift.h" statement 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) Verify that "ProductModuleName-Swift.h" is generated by Cmd-Clicking on the class name of the #import "ProductModuleName-Swift.h" statement Remove the comment on the code referencing Swift classes in the Objective-C implementation file. Clean & Build as normal (the "ProductModuleName-Swift.h" should be generated and your Objective-C code referencing Swift Classes can be used as normal)

注意:在执行此过程时,关于将空格更改为下划线和将定义模块更改为YES的答案仍然适用,正如Apple文档中指定的规则一样。

桥接头路径

在一个错误中,在构建过程中没有找到文件ProductModuleName-Bridging-Header.h。这一事实产生了一个错误

< unknown>:0:错误:桥接头 “/Users/Shared/Working/abc/abc- bridging - header .h”不存在

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

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

如果Xcode实际上生成了你的-Swift.h头(在DerivedData的深处),但它没有引用你的Swift类,请确保你也定义了一个桥接头。我阅读文档的方式暗示我只需要从Swift调用Objective-C,但似乎从Objective-C调用Swift也是必要的。

请看我的回答:https://stackoverflow.com/a/27972946/337392

编辑:这是因为公共和内部访问修改器,正如我最终在苹果文档中发现的解释:-

默认情况下,生成的头包含Swift接口 用公共修饰符标记的声明。它还包含了 如果你的应用目标有 Objective-C桥接头。