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: "
- Under Build Settings, in Packaging, make sure the Defines Module setting for the framework target is set to Yes.
-
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.
eskimo
(Quinn “The Eskimo!”)
2
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:
-
Using Xcode 15.4 on macOS 14.5, I created a project called Test73168 from the macOS > App template.
-
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.
-
Under the FFF group in the Project navigator, I created a new Objective-C class called FFFWaffle.
-
I selected FFFWaffle.h and, in the File inspector, in the Target Membership slice, change the popup to Public.
-
In FFF.h, I added the line:
#import <FFF/FFFWaffle.h>
-
I switched to the FFF scheme and built it.
-
I then switched back to the Test73168 scheme.
-
In one of the app’s Swift files, I added this code:
import FFF
func test() {
let w = FFFWaffle()
print(w)
}
-
I built the app. That worked.
-
I added a button that calls my test() function.
-
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.
eskimo
(Quinn “The Eskimo!”)
4
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