简体   繁体   中英

How to import Objective-C file to bridging header if it contains an import of Project-Swift.h

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?

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, eg @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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM