0
votes

I am having trouble generating the swift header needed to use Swift classes from ObjC, when there are multiple targets with common code.

I created two targets My OSX App and My iOS App. These targets share common code where an ObjC class is calling a Swift class.

As described in Swift and ObjC in the same project I can add #import "My_iOS_App-Swift.h" to my ObjC class and compile it from the My iOS App target.

However, this doesn't compile from the My OSX App target, as the include needs to match the module name. It is looking for #import "My_OSX_App-Swift.h"but the common code does not use that include.

What is the correct way to mix/match Swift/ObjC in code that is shared between multiple targets? I could manually change every target to use a common MyApp-Swift.h, but that doesn't feel right and may cause other problems.

1
Use a pod or a module for code that's not platform specific. It shouldn't by My_iOS_App-Swift.h or MyApp-Swift.h. It should by MyLibrary-Swift.hnhgrif
Yes, that makes sense however I inherited this project and it wasn't set up that way. Can I manually change the name of the generated -Swift.h so each target uses the same one without causing problems? Restructuring the projects to use a library may be too much change at the moment. (I would like to accept your answer but can't from a comment)Jim Leask

1 Answers

3
votes

Set the Product Module Name setting in Build Settings to be the same across your modules

For example: $(PROJECT_NAME)

Or use fixed names if you have watch extensions for different targets like: Main_App and Watchkit

This causes the ###-Swift.h file that is generated has the same name across all modules. This also eliminates the need for adding/checking preprocessor micros.

From: Objective C to Swift header file with multiple targets