Problem using Swift with Objective-C framework

I have an old Objective-C project, which has multiple targets, and some of the targets share a framework (let's call it CJDataKit) that's also written in Objective-C. I'm trying to add some Swift code to my project, at least to the main app target, and have some limited implementation of it working, but I'm running into some issues whenever the Swift code needs to use or import the CJDataKit framework, or any header file that itself imports the CJDataKit framework.


What's Working

  • I wrote a basic UIView subclass in Swift (which didn't need any other code from my app), and I can use this in my Objective-C target, using @objc keyword and by importing "MyApp-Swift.h" in my Objective-C code.
  • I then wrote a new UIViewController subclass in Swift, and used a couple of simple Objective-C objects from both CJDataKit framework and non-framework classes. I did this by creating a bridging header file, and added the Objective-C headers there.

So far so good, and everything compiles fine.



What's Not Working

The problem happens if I try to import into Swift other Objective-C files that might be importing the CJDataKit framework inside it. For e.g. I wanted to write a Swift extension of an existing UIViewController subclass (call is PageAViewController). This PageAViewController imports multiple other header files. If I add it to the bridging header file, I start getting build errors:

Include of non-modular header inside framework module 'CJDataKit': '.../CJDataKit/Person.h'

Commenting out the

@import CJDataKit
makes it work for this particular file, but it still gets compile errors from a different header file (that was imported by PageAViewController). It only seems to work if all the files listed in the bridging-header file don't have a
@import CJDataKit
, which is difficult and cumbersome. So something about Swift doesn't like interacting with the CJDataKit framework directly.


I've also tried importing the CJDataKit.h header file into the Swift bridging header file, figuring this way I don't have to individually import each file from the framework, but that doesn't work either. That results in a different error:

Could not build module 'CJDataKit.h'

I've tried using

#import <CJDataKit/CJDataKit.h>
as well but same result.


From my settings:
- "Allow Non-modular Includes In Framework Modules" is Yes on the target, and framework. It is No at the project-level.
- "Defines Module" is also set to Yes, on both the app target and framework, and No at project-level.


Would love some help in getting this setup correctly. I've been searching for a solution, but haven't really found anything.

It sounds like the CJDataKit framework headers aren’t modular. They need to be if you’re going to use them from Swift. This shouldn’t be difficult; it’s likely that there’s a small oddity in the way that your framework set up that’s triggering the headers to be non-modular.

I generally approach these problem by creating a small test project, figuring out what to do that, and then transferring that knowledge over to my main project. In this case, you could create a small Swift app with an Objective-C framework target and confirm that the Swift app can import the Objective-C framework. You can then add Objective-C code to the app to confirm that it also can access the framework. Once you have that running, you can compare the configuration of this test framework to that of your real framework to try to isolate what’s causing the problem there.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Problem using Swift with Objective-C framework
 
 
Q