I'm trying to work on a mixed Swift and ObjectiveC project with no luck.
My project is made of 6 targets:
- App
- Core
- CoreTest
- Summary
- WatchKit Extension
- WatchKit App
I've been able to add a Swift class that has one target membership (App) and uses ObjectiveC code passing from the App-Bridging-Header.h
The "ObjectiveC into Swift code" paradigm works like a charm.
Instead, I'm struggling with the "Swift into ObjectiveC code" paradigm.
In the specific case I need to use a new Swift class inside an ObjectiveC .m file that is member of four targets.
What I've been doing so far is this:
- Add a new Swift file to the Core project specifying the four memberships (App, Core, Summary, WatchKit Ext)
- XCode asks to create -Bridging-Header.h for three projects Core, Summary and WatchKit Ext (App-Bridging-Header.h already exists since it was used previously), and I consent (don't know why it creates those files inside the same group folder where I add my Swift class but nevermind)
- I create the Swift class adding the @objc key before class
- I go check if Objective-C Generated Interface Header Name is set for all modules, and yes it is
- I go check if Defines Module is set to No in all modules and set to Yes in the main project, and yes it is
- I write down the imports for the -Swift.h files for all the four targets
- Inside my Objective-C class I try to use my new Swift class, apparently it seems to work (even autocompletion)
- I try to build the project but it fails saying that the -Swift.h file is not found
I've followed the Mix and Match guide of Apple and many SO threads including this one that seems apparently to address the same problem of mine but it doesn't work.
Also have seen this and this but didn't work.
Have tried also to Delete Derived Data as well as cleaning the whole project several times with no luck.
EDIT 1 : More details
This is the declaration of my Swift class
import RealmSwift
@objc class MyClass: Object {}
This is the snippet of usage of the Swift class inside ObjectiveC
MyClass *app = [[MyClass alloc] init];
If I avoid using #import ...-Swift.h the build fails saying that I'm making use of an Undeclared idenfier "MyClass" and this is sound.
If I do use #import ...-Swift.h files it says that module X cannot find module Y and so on.
EDIT 2 : Swift module name
By looking at the SWIFT_OBJC_INTERFACE_HEADER_NAME property of each target I've seen that it is built using this syntax $(SWIFT_MODULE_NAME)-Swift.h do I need to change SWIFT_MODULE_NAME to MODULE_NAME?
EDIT 3 : Imports
This is the set of imports for Swift headers in .m ObjectiveC file.
I'm using the module names specified in relative Build Settings properties. Notice that one has '_' character since WatchKit Extension target has a space in the target name.
#import "ProjectName_App-Swift.h"
#import "ProjectName_Core-Swift.h"
#import "ProjectName_Summary-Swift.h"
#import "ProjectName_WatchKit_Extension-Swift.h"
EDIT 4 : Cross visibility of -Swift.h files
I've tryed the following:
- Added a Swift class only in target App
- XCode created the Bridging Header
- Inserted #import "ProjectName_App-Swift.h" in ObjectiveC file used only by App
- Made use of Swift class inside this file
- It compiles! And I'm able to use the Swift class
When the Swift class is defined for multiple targets and ObjectiveC file is used by those multiple targets it doesn't work. The build errors are like this: Target X -> "ProjectName_TargetY-Swift.h" file not found.
Seems like a target cannot see other targets -Swift.h files.
What am I doing wrong?
Thanks for your help
See Question&Answers more detail:
os