Objective C to Swift header file with multiple targets

Objective CSwiftHeader FilesTarget

Objective C Problem Overview


I'm successfully calling my Swift classes from Objective C (for target 'MyApp') via the import statement:

#import "MyApp-Swift.h"

I've now created a new target called "MyAppLite"

When I compile the new target, I get errors because "MyApp-Swift.h" is required by the code, but the compiler is creating "MyAppLite-Swift.h"

So, I need to create a conditional Swift/ObjC header #import for the target I'm compiling against.

How can this be done, or is there a better way?

Objective C Solutions


Solution 1 - Objective C

It is also possible to set the Product Module Name setting in Build Settings to be the same across your modules (I set it to $(PROJECT_NAME)), so that the <project>-Swift.h file that is generated has the same name across all modules. This eliminates the need for adding/checking preprocessor macros.

Solution 2 - Objective C

The best way I've found to address this issue is in your Xcode shared project settings. You can search for the following setting:

Objective-C Generated Interface Header Name*

If you set that value to a common header name, such as "MyProject-Swift.h", it will be applied to each of your targets and you can safely use the import declaration in any of your Objective-C files. This allows you to continue using unique product module names for each target, if needed.

I've tested this in Xcode Version 6.4 (6E35b).

*Note: This will appear under your Swift compiler settings, which are only visible if you have Swift source files added to your project. Additionally, if a target doesn't have any Swift source associated with it, the Swift compiler options will not be visible for that target's build settings.

Good luck!

Solution 3 - Objective C

Previous answers have some problems if you decide to rename your targets or project, or use SWIFT_MODULE_NAME as intended.

The most universal solution is to change SWIFT_OBJC_INTERFACE_HEADER_NAME (“Objective-C Generated Interface Header Name”) under Project's, not Targets, Build Settings, to:

  • $(PROJECT_NAME)-Swift.h — one per project;
  • $(SWIFT_MODULE_NAME)-Swift.h — one per module (default value).

enter image description here

Solution 4 - Objective C

Well, the only way I can fix is to...

#ifdef IS_LITE
#import "MyApp_Lite-Swift.h"
#else
#import "MyApp-Swift.h"
#endif

Note that if there's any 'illegal' chars in my Product Module Name, they need to be replaced with underscores.

Hope that helps!

Solution 5 - Objective C

I put the appropriate #import <project>-Swift.h statement in a prefix header file (<project>-Prefix.pch) defined/added for each build (target/scheme).

Solution 6 - Objective C

The only working way is the following :

1- from first target (which has a working bridging) Build Setting select Objective C Bridging Header
2- Copy Objective C Bridging Header
3- open the other target Build Setting
4- Paste it 5- change the header file to your new header file (i.e target B.h)

(now you have this option for two target)

Solution 7 - Objective C

> pickture will tell you all

  1. second targets name xxx and xxx-ih

enter image description here

  1. select first target tap build setting -> find Objective-C Bridging Header set xxx-bridging-Header.h and Objective-C Generated Interface Header Name set xxx-Swift.h enter image description here

  2. select second target and same step 2 enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFittoburstView Question on Stackoverflow
Solution 1 - Objective CC0D3LIC1OU5View Answer on Stackoverflow
Solution 2 - Objective CMike FayView Answer on Stackoverflow
Solution 3 - Objective CDmitry IsaevView Answer on Stackoverflow
Solution 4 - Objective CFittoburstView Answer on Stackoverflow
Solution 5 - Objective CWillView Answer on Stackoverflow
Solution 6 - Objective CAhmad AlbashView Answer on Stackoverflow
Solution 7 - Objective CRGB_DSView Answer on Stackoverflow