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

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

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

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

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


当前回答

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

其他回答

这个答案解决了你可能已经有一些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文件再次自动生成。

最重要的是这个文件是不可见的!!至少在Xcode6 beta5中是这样的。在您的工作空间中不会有这样一个名为“YourModule-Swift.h”的文件。只需确保模块名称和定义模块设置为yes,并在Objective-C类中使用它。

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

但你的项目名称的最终答案,包括所有的特殊字符(是它'。’或数字或空格)-你可以在目标的Build Settings下的“Product Module Name”中找到适合你的文本。

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

所以我使用#import "_mg-Swift.h",它工作了。

项目中不创建实际文件([ProductModuleName]-Swift.h)。Cmd +单击导入,要么实时生成(并在内存中),这样你就可以看到链接是如何完成的,要么在Xcode缓存目录中打开一个文件,但它不在项目目录中。

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

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

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

#import "Test_Project-Swift.h"

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.