Importing Objective-C framework in a Swift Framework

Hi, I am trying to make a Swift framework that uses an Objective-C framework. But it doesn't work it says that "Could not build objective C module". Since it is a framework bridging header doesn't work.

The official apple documentation says: "

  1. Under Build Settings, in Packaging, make sure the Defines Module setting for the framework target is set to Yes.
  2. In the umbrella header, import every Objective-C header you want to expose to Swift." - [Developer.Apple](https://developer.apple.com/documentation/swift/importing-objective-c-into-swift)
    

However, that doesn't work either. Kindly guide me how to make it work.

In situations like this I like to start with a test project that exercises the minimal test case. If I can’t that working, then the test project makes it easy to ask for help (or file a bug). OTOH, if I that works then I have a known good example I can compare to my real project that’s having problems.

With that I mind, I did the following:

  1. Using Xcode 15.4 on macOS 14.5, I created a project called Test73168 from the macOS > App template.

  2. Within that, I created a new target from the macOS > Framework template. I set the name to FFF, chose Objective-C as the language, and chose to embed it in the Test73168 app.

  3. Under the FFF group in the Project navigator, I created a new Objective-C class called FFFWaffle.

  4. I selected FFFWaffle.h and, in the File inspector, in the Target Membership slice, change the popup to Public.

  5. In FFF.h, I added the line:

    #import <FFF/FFFWaffle.h>
    
  6. I switched to the FFF scheme and built it.

  7. I then switched back to the Test73168 scheme.

  8. In one of the app’s Swift files, I added this code:

    import FFF
    
    func test() {
        let w = FFFWaffle()
        print(w)
    }
    
  9. I built the app. That worked.

  10. I added a button that calls my test() function.

  11. I ran the app and clicked that button. It printed:

    <FFFWaffle: 0x6000027348a0>
    

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

3 Likes

Hey, thanks for the reply!

However, in your test case you are using an Objective-C framework (FFF) in a Swift App(Test73168). What I need to do is Make a Swift framework that uses Objective-C that in turn will be used in an app.

What I need to do is Make a Swift framework that uses
Objective-C that in turn will be used in an app.

I don’t think that’s substantially more difficult than this. The key point of the above is that the Objective-C framework is a module and thus can be imported directly into Swift. It doesn’t rely on the bridging header mechanism that only works in app targets.

I recommend that you work through the above process, then adapt it to use a new test Swift framework, and then apply your new-found expertise to your real project.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

1 Like

thanks it worked!

1 Like