Include -Swift.h in the umbrella header

I have a mixed Swift project and Objective-C. In order to access the Swift public API in Objective-C, the consumer API must currently import "MyModule-Swift.h". I already have an Objective-C header called "MyModule.h". But importing "MyModule.h" will not work for the Swift API. I tried to import "MyModule-Swift.h" into the umbrella header, but it does not find it (I assume, because it is generated "on the fly" by Xcode).

Any suggestions that the consumer of the API could always import "MyModule.h" to use public APIs written in Swift / Objective-C would really be appreciated.

==================================================== ===================

Change I apologize for not taking the time to ask the right question. I have a MyModule framework.

I have an Objective-C class called ABUser ,

 @interface ABUser: NSObject - (void)walk; @end 

I wanted to add new behavior to this class using Swift, so I defined an extension

 extension ABUser { func swiftWalk() } 

Now, let's say I wanted to access the swiftWalk method defined in ABUser from an Objective-C application, I would have to #import <MyModule/MyModule-Swift.h> . #import <MyModule/MyModule.h> will work if I want to use the walk method. This assumes that the header header of MyModule.h imports ABUser.h .

I always wanted the Objective-C application #import <MyModule/MyModule.h> and never worry about whether the API was written in Objective-C or Swift as part of the framework. So, I tried to import MyModule-Swift.h into the umbrella header MyModule.h . But my Objective-C application did not compile if I did. Not sure if this is because MyModule-Swift.h generated by Xcode on the go during the build process.

Change 2 . A sample project has been added to reproduce this problem here: https://github.com/priteshshah1983/MyObjectiveCApp

The corresponding code is in ViewController.m . The line ends with the master branch. To make it work, open the working branch.

+8
ios objective-c swift
source share
2 answers

The reason @import MyModule; , is that "the modules are packaging along with the executable file of the framework and the headers" ( from this SO place ).

In other words, when you @import MyModule; , Objective-C will automatically import all the fast and Objective-C headers associated with the module, while you cannot include the fast header from the umbrella header. I suggest you take a look at the differences between @import and #import in a related SO answer.

+2
source share

It turns out using @import MyModule; instead of #import <MyModule/Module.h> allows you to use the walk and swiftWalk , as expected, from an Objective-C application.

Honestly, I don’t understand the details, but I hope this helps someone else. Please feel free to explain!

-one
source share

All Articles