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

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

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

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

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


当前回答

我发现了一个对我很管用的小窍门。

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? Command+shift+k to clean your code, if you receive an error about your "ProductModuleName-Swift.h" delete it from appDelegate.h file. Clean your code again. Now everything will work like a charm If you receive again error about the "ProductModuleName-Swift.h", now create again in appDelegate.h file and clean your code again.

每次你收到这个错误的时候都要做这个工作(从appDelegate.h文件中删除并创建“ProductModuleName-Swift.h”,然后清理你的代码)来静音它。

其他回答

项目必须有一个不包括空格的模块名。 “定义模块”必须在“生成设置”中的“打包”下设置为“是”。 注释掉了#import语句:

如果您在导入“ProductModuleName-Swift.h”时仍然有错误,那么

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

这显示了我的Swift代码中的一堆其他错误。

一旦我修复了这些新的错误,并成功地获得了源代码构建,我取消了#import的注释,没错!头文件被创建并正确导入:)

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

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.

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

对我来说,问题是这是一个先有鸡还是先有蛋的问题,因为在我注释掉#import语句之前,我没有看到任何构建错误:

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

这显示了我的Swift代码中的一堆其他错误。

一旦我修复了这些新的错误,并成功地获得了源代码构建,我取消了#import的注释,没错!头文件被创建并正确导入:)

有时你只需要在obj-c .m文件上取消设置,然后重新设置目标成员。