Unknown type name for swift property

2019-01-23 05:36发布

问题:

I have a CustomViewController class written in swift and a CustomNavigationController class written in Objective C. I'm trying to add my CustomNavigationController as a property to my CustomViewController. I've added #import "CustomNavigationController.h" to my bridging header.

In my CustomViewController I have:

class CustomViewController: UIViewController {

    var navController: CustomNavigationController?
...
//init methods


...


 override func viewDidLoad() {
        super.viewDidLoad()

        //Set up Navigation Controller
        navController = self.storyboard.instantiateViewControllerWithIdentifier("CustomNavigationController") as CustomNavigationController!
}

There are no errors until I try to build and run...I get "unknown type name 'CustomNavigationController'; did you mean 'UINavigationController'?"

Does anyone know why it doesn't recognize the type?

回答1:

In your Objective-C code, you are somewhere importing the automatically generated -Swift.h header. In that same code, before that #import line, insert #import "CustomNavigationController.h". The order of these two #import statements is crucial!

This will solve the problem by making sure that CustomNavigationController is in the namespace before the automatically generated -Swift.h header, and so the latter will know about the former and all will be well.

This is something of an annoyance if that Objective-C class had no need to know about CustomNavigationController, but it solves the problem going forward and allows you to continue to work with your hybrid project.



回答2:

Looks like the ProjectName-Swift.h generated header file doesn't automatically include the contents of ProjectName-Bridging-Header.h. This causes any types that haven't already been declared before importing ProjectName-Swift.h to throw the Unknown type name error in the compiler. This seems like a bug.

My workaround was to create an alternate version of ProjectName-Swift.h that forward declares the classes that are causing the errors, then imports ProjectName-Swifth.h. I called it ProjectName-Swift-Fixed.h. For me, ProjectName-Swift-Fixed.h looked like this:

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

Then, everywhere in code where I had #include "ProjectName-Swift.h", I replaced it with #include "ProjectName-Swift-Fixed.h"



回答3:

In case you can´t fix the problem by changing the order of #import statements as suggested by the above answers, checking the files in your ProjectName-Bridging-Header.h for missing framework imports might work.

In my case, I had a class in the bridging header file that was using UIImage in one of it´s methods. When my project consisted solely of Objective-C that worked fine but when exposing this header to Swift I had to add #import <UIKit/UIKit.h> in order to remove the error.



回答4:

Try this:

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


回答5:

I encountered the same situation. In my case, the errors resolved after updating swift version to 3.0 in all targets by Edit > Convert > To Current Swift Syntax. Hope it helps



标签: ios swift xcode6