dmoss18
dmoss18

Reputation: 867

How to add import to generated module-swift.h file?

I know the ProjectName-Swift.h file is generated automatically. But is there a build setting somewhere that will let me add some custom code to it? Specifically I need to add an #import SomeFile.h

I have a class in my -Swift.h file that is causing compiler errors because it's implementing an Objective-C protocol (from a 3rd party library) but the -Swift.h file isn't importing the header for that protocol.

Example:

//Swift
public class MyClass: NSObject, MyObjectiveCProtocol { ... }

//ProjectName-Swift.h file
SWIFT_CLASS("_TtC11ProjectName14MyClass")
@interface MyClass : NSObject <MyObjectiveCProtocol> ...

The error says "Cannot find protocol declaration for 'MyObjectiveCProtocol'" So I add #import 'MyObjectiveCProtocol.h to the -Swift file, but it gets removed every time I clean and build.

Is there any way that the import can be generated automatically instead of me having to do it manually? I tried adding it to the prefix file, but that felt wrong (even though it worked).

Upvotes: 1

Views: 267

Answers (1)

John Montgomery
John Montgomery

Reputation: 7106

If you want to use Objective-C code in Swift, you need to add the #import to -Bridging-Header.h, not -Swift.h. As far as I know, there isn't any situation where you should be trying to manually modify -Swift.h.

See here for more info.

Upvotes: 0

Related Questions