0

So, problem goes as is. I have a file that contains:

#import "Project-Swift.h"

And I want to add this file to my BridgingHeader. So when I try to compile it, the error is as follows:

'Project/Project-Swift.h' file not found
#import "Project-Swift.h"
        ^
<unknown>:0: error: failed to import bridging header 'path/to/my/folder/Project/BridgingHeader.h'

I can not remove #import "Project-Swift.h" from this .h file as it's needed there. And I also want to use this Objc file in Swift. What are the options?

Vivienne Fosh
  • 1,751
  • 17
  • 24

1 Answers1

1

This can be solved by including the Project-Swift.h header in the implementation .m file instead of the header file, and by using forward declarations in the header, e.g. @class SomeClass, @protocol SomeProtocol.

The solution is identical to the Objective-C solution when you have two classes depending one to each-other.

For example, given the following header file:

#import "Project-Swift.h"

@interface MyObjcClass: NSObject

@property SomeSwiftClass *aProperty;
@property id<SomeSwiftProtocol> delegate;

and the .m file like this

#import "MyObjClass.h"

@implementation MyObjcClass
...

, you need move the #import "Project-Swift.h" into the .m file, and update your header file like this:

@class SomeSwiftClass;
@protocol SomeSwiftProtocol;

@interface MyObjcClass: NSObject

@property SomeSwiftClass *aProperty;
@property id<SomeSwiftProtocol> delegate;

@end

and the .m file like this:

#import "Project-Swift.h"
#import "MyObjClass.h"

@implementation MyObjcClass
...

Note that you'll likely need to place the "Project-Swift.h" import before the one for your class header import if the objective-c class is declared to implement one of the Swift declared protocols.

Cristik
  • 30,989
  • 25
  • 91
  • 127
  • Yeah, thanks, the only small issue is that it shows warning: Cannot find protocol definition for 'ARTPasscodeKeyboardControllerDelegate' – Vivienne Fosh Jan 21 '16 at 18:36
  • Have you added the forward declaration for the protocol, and included its header in the `.m` file? You might need to add that header before the one where you define your class, if the protocol is part of the class definition. – Cristik Jan 21 '16 at 18:37
  • the matter is - this protocol is declared in swift file. I have added @protocol declaration but it still shows warning – Vivienne Fosh Jan 21 '16 at 19:18
  • Did you imported the headers in the order from my answer? i.e the -swift one first? – Cristik Jan 21 '16 at 19:24
  • Yeah, swift one first. If I write this: @property (nonatomic) id delegate; it's fine, but the matter is this is a swift protocol and forward declaration shows warning as shown here: https://www.dropbox.com/s/70qfj95qnvaq7jj/Screenshot%202016-01-21%2021.44.46.png?dl=0 – Vivienne Fosh Jan 21 '16 at 19:44
  • Is ARTPasscodeKeyboardControllerDelegate marked as available for Swift? – Cristik Jan 21 '16 at 19:47
  • like this: @objc protocol ARTPasscodeKeyboardControllerDelegate – Vivienne Fosh Jan 21 '16 at 22:04