Import a Swift module in Obj C module

865 Views Asked by At

I have a xcworkspace with two xcodeprojects inside (one is a static library where all the base functionalities are implemented and the other is a demo project which uses classes from first one) - and all the code so far has been written in Objective C. What I want to achieve is create a Swift class in the static library and then access it from an Objective C class in the 'demo' xcodeproj.

I have created this 'Test.swift' class and a bridging header that was created automatically (changed the Defines Module property to YES in the Build setting), and everything works well - I can access it from Obj C classes in the same project. Next, I am creating a new "DemoTest.swift" Swift class in the 'demo' project and subclassing the Test.swift (which works). However, when trying to access this class from an Objective C file in the 'demo' project, the compiler doesn't recognize my "base" module -

In file included from 
xxxxx/AppDelegate.m:26:
xxxxx/mpdemo-Swift.h:189:9: fatal error: module 'mpbba' not found
@import mpbba;
 ~~~~~~~^~~~~
1 error generated.

My question is how can I have a Swift code imported into another Objective C module?

1

There are 1 best solutions below

1
On

Here's what to do:

  1. configure an Objective-C bridging header
  2. Click on your Xcode Project file
  3. Click on Build Settings
  4. Find the Search bar and search for Defines Module.
  5. Change the value to Yes.
  6. Search Product Module Name.
  7. Change the value to the name of your project.
  8. In class, add the following: #import "YourProjectName-Swift.h"

Hope this will work. let me know if you need anything else.