Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown type name for swift property

Tags:

ios

swift

xcode6

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?

like image 840
Nick Yap Avatar asked Jun 07 '14 16:06

Nick Yap


3 Answers

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.

like image 74
matt Avatar answered Oct 04 '22 01:10

matt


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"

like image 36
johnboiles Avatar answered Oct 04 '22 03:10

johnboiles


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.

like image 35
Lucifer Avatar answered Oct 04 '22 03:10

Lucifer