Jim Leask
Jim Leask

Reputation: 6429

Swift generated header with multiple targets

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.

Upvotes: 1

Views: 1576

Answers (1)

Sjoerd Perfors
Sjoerd Perfors

Reputation: 2317

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

Upvotes: 5

Related Questions