未知的types名称为swift属性

我有一个用Swift编写的CustomViewController类和用Objective C编写的CustomNavigationController类。我试图将我的CustomNavigationController作为一个属性添加到我的CustomViewController中。 我已经添加#import "CustomNavigationController.h"到我的桥接头。

在我的CustomViewController中我有:

 class CustomViewController: UIViewController { var navController: CustomNavigationController? ... //init methods ... override func viewDidLoad() { super.viewDidLoad() //Set up Navigation Controller navController = self.storyboard.instantiateViewControllerWithIdentifier("CustomNavigationController") as CustomNavigationController! } 

没有错误,直到我尝试构build和运行…我得到“未知的types名称”CustomNavigationController“;你的意思是”UINavigationController“?

有谁知道为什么它不承认这种types?

在你的Objective-C代码中,你在某处导入了自动生成的-Swift.h头文件。 在相同的代码中, #import之前 ,插入#import "CustomNavigationController.h" 。 这两个#import语句的顺序是至关重要的!

这将通过确保-Swift.h在自动生成的-Swift.h头部之前的命名空间中来解决问题,所以后者将知道前者,并且一切都会好的。

如果Objective-C类不需要了解CustomNavigationController,这是一件烦恼的事情,但它解决了前进中的问题,并允许您继续使用混合项目。

看起来像ProjectName-Swift.h生成的头文件不会自动包含ProjectName-Bridging-Header.h的内容。 这会导致在导入ProjectName-Swift.h之前尚未声明的任何types在编译器中引发Unknown type name错误。 这看起来像一个错误。

我的解决方法是创buildProjectName-Swift.h的替代版本, ProjectName-Swift.h声明导致错误的类,然后导入ProjectName-Swifth.h 。 我把它叫做ProjectName-Swift-Fixed.h 。 对我来说, ProjectName-Swift-Fixed.h看起来像这样:

 // ProjectName-Swift-Fixed.h @class CustomViewController; #import "ProjectName-Swift.h" 

然后,在我有#include "ProjectName-Swift.h"代码中,我用#include "ProjectName-Swift-Fixed.h"取代了它。

如果你不能通过改变上述答案build议的#import语句的顺序来解决问题,检查ProjectName-Bridging-Header.h中缺less框架导入的文件可能会起作用。

在我的情况下,我在桥接头文件中有一个类,在它的一个方法中使用UIImage 。 当我的项目完全由Objective-C组成,工作正常,但是当把这个头文件暴露给Swift的时候,我不得不添加#import <UIKit/UIKit.h>来清除错误。

尝试这个:

 navController = self.storyboard.instantiateViewControllerWithIdentifier("CustomNavigationController") as? CustomNavigationController 

我遇到了同样的情况。 在我的情况下,通过编辑>转换>到当前Swift语法,在所有目标中将swift版本更新到3.0后,错误得到解决。 希望能帮助到你