I'm trying to separate out some Swift @objc protocols from a mixed Swift/Obj.C framework "Utils" into a pure Swift local SPM package "Adapters", which declares a static library target that gets imported by the framework.
However, now the Obj. C code in Utils apparently doesn't recognize any of the @objc protocols that I moved into Adapters.
When the Swift @objc protocols were part of the same target as the Obj. C code itself, all I had to do is #import <Utils/Utils-Swift.h>
in the Obj. C .m files and use forward declarations in the .h files.
Now that the Swift @objc protocols are part of the Swift Package however, I cannot simply replace #import <Utils/Utils-Swift.h>
with @import Adapters
. It won't compile, and I get confusing messages that make it seem like the Obj. C classes and properties no longer conform to the protocols that were moved into Adapters.
What am I doing wrong? Is there something else that I need to do in order to get forward declarations of @objc protocols from the Swift package to work?
I tried adding cSettings: [.define("OTHER_LDFLAGS"), to: "-ObjC")]
to the Swift package, to no avail. It doesn't seem to matter if I declare the package's product as a dynamic or static library. I also tried adding @_exported import Adapters
to a file in Utils, but it didn't help.
I've done quite a bit of googling, and found several cases of other people having similar sorts of issues, and I tried a variety of possible solutions. But so far nothing helped and I have not been able to find a workaround:
- this unanswered StackOverflow asking the same question: https://stackoverflow.com/questions/60423384/generating-objective-c-header-with-swift-package-manager
- this unanswered StackOverflow asking the same question: swift package manager - Exporting public C headers with SwiftPM - Stack Overflow
- this older Swift Forums post indicates what I'm trying to do is impossible: Using a Swift Package in a mixed Swift and Objective-C project - #2 by Aciid. A later post in the thread claims the support was added in Xcode 11.4, and another later post seems to indicate using
@import Foo;
should just work, however it doesn't work for me
Any help or advice would be most welcome! Thanks
Note: The only workaround I found so far is to perform the following steps:
- Create an Xcode project from the Swift Package using
swift package generate-xcodeproj
- Link Utils to the Adapters.framework defined by that Xcode project
- Add a Copy Headers build phase to Adapters framework
- Add a public header
Adapters.h
to the Adapters framework - Add
#import <Adapters/Adapters-Swift.h>
to the public headerAdapters.h
Now, the build succeeds. However my understanding of Swift packages is that you cannot have a mixed Swift & Obj.C SPM package. Even if you could, I don't know how to add the equivalent of a Headers build phase to a Swift package. Please advise, thanks!
Note 2: When I inspect the Adapters.framework that gets built from the Swift package, it's always missing a Headers folder, even if I add publicHeadersPath: "Headers"
to Package.swift. This seems to be the reason that Obj. C in other modules is unable to see the compatibility header, but I don't know what to add to the Package.swift file to get Headers to be properly added to the framework.