objective-cswift3bridging-header

Adding Swift Class to Objective-C Project with Multiple Targets


I have an existing Obj-C project that houses many targets that all share the same AppDelegate. I want to bridge a swift class that is used by select targets. I can do this easily when I have one target.

When I add a swift file to the project, I select the desired targets and the necessary bridging-header.h files are generated, but when I try to import those -swift.h files, they are can't be found.

Are there steps I'm missing when it comes to projects that have multiple build targets?

EDIT - More Details

I wanted to add a little bit more detail on how my project is set up.

enter image description here

I have a Framework, we'll call it AppFactory, coded in Obj-C. I have multiple build targets that generate different versions of the Core app depending on information in that target's plist. I want a swift file to be utilized by these apps. In my build settings the Defines Module is marked to Yes, I have create this swift class:

@objec class SwiftClass: NSObject { }

And in doing that, Xcode generated the proper Briding-Header.h files.

According to Apple Guides when you have multiple build targets your import header should include the ProductName/ProductModuleName-Swift.h, which should be auto generated by Xcode.

When I look in to my derived data folder, the ProductModuleName-Swift.h does exist, with in each targets build folder, under the AppFactoryCore folder.


Solution

  • I found a solution to my problem.

    Swift File Targets: Instead of having SwiftClass.swift target both the framework and the selected targets (AppA, AppB & AppC), I backpedaled and solely targeted the framework, AppFactoryCore.

    Build Settings (Packaging > Defines Module): I reverted each app target's Defines Module property from YES to NO, and set this property to YES for the framework target.

    Swift Class Declaration: The guide states:

    Because the generated header for a framework target is part of the framework’s public interface, only declarations marked with the public or open modifier appear in the generated header for a framework target.

    So I added access control modifiers to my class and class functions

    @objc open class SwiftClass: NSObject {
        //Code
    }
    

    Import Header: Since SwiftClass.swift is only targeting the framework target, and it is in fact a framework that is being used, the header import SwiftClass.swift into the universal AppDelegate was

    #import <AppFactoryCore/AppFactoryCore-Swift.h>
    

    Which finally became visible once all previously stated modifications were done.

    Now that the file is global to all targets I added a custom attribute to identify if the target running was is one that should utilize SwiftClass.swift.

    Hope this helps anyone else trying to accomplish a relatively similar task!