9

In an iOS application I have a subproject (not cocoapods) in which I have included a swift file and ObjC file (that is used by the swift file). XCode automatically created a bridging file but could not build it because apparantly bridging is not allowed in a framework. The workaround that I used was to add the objective-c header to the umbrella file and it worked. Now I need to use a swift class from ObjC. I have define module to set to YES, the generated file Framework-Swift.h . But when I try to import it in objective-c i get

Could not build Objective-C module

The closest I got after some googleing was this answer:

Ah gotcha. It looks like you're building a mixed Swift & Objective-C pod - if that's the case, Xcode will try to import within the generated -Swift.h header. You'll need to create the header manually and add imports for the Objective-C classes that you want to expose to Swift.

CocoaPods generates an umbrella header automatically and imports it within the .modulemap, but Xcode doesn't use that when generating the -Swift.h header

But I am unsure what header needs to be created manually. Any ideeas or pointer about using swift in an objective-c framework ? In both ways ?

3 Answers 3

15

I also had similar issue when using Swift pods in my Swift project containing several targets. No Objective-C code at all. I tried to clear build folder, pods cache, derived data - nothing worked.

Solution:

Open the Build Settings for a target that contains your module code. Set the "Install Objective-C Compatibility Header" to "No"

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

For me the answer was to set it to NO then set it back to YES.
0

There's a great and simple article that wraps up this case:

  1. DEFINES_MODULE=YES
  2. To use ObjC classes in Swift, create a bridging header and specify path to it in the build settings
  3. To use Swift classes in ObjC, #import <ModuleName/ModuleName-Swift.h> in your *.m file (use @class SwiftClass; forward declaration in *.h file, if needed)
  4. For swift classes and their members to be visible to objc
    1. inherit your class from NSObject
    2. prepend it with @objcMembers
    3. make both the class and its members public

Comments

0

In Xcode 16, .h file's Target Membership should be set as Public. Select the header file, open the Inspector window located right side, and set Target Membership as Public.

Could not build Objective-C module solution

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.