3

I would like to create a Swift framework which links to Objective-C code.

The problem is that when I use bridging between my Swift code and the Objective-C code, compilation of the framework fails with "Using bridging headers with framework targets is unsupported".

The only solution I found (edit: similar to this one) was to import in my framework's umbrella header every Objective-C header that should be exposed to Swift, and then make these headers public.

As a result, I'm able to build a framework, but it contains all the Objective-C code. What I would like to achieve is to produce a framework which contains only the Swift part.

Ariel Malka
  • 15,697
  • 6
  • 31
  • 33
  • Possible duplicate of [Objective C Bridging Header for frameworks](https://stackoverflow.com/questions/24841144/objective-c-bridging-header-for-frameworks) – l'L'l Jan 28 '19 at 18:43
  • @l'L'l This is not a possible duplicate. I know about this question and the chosen answer but when I try it, I'm getting a framework which contains Swift and Objective-C. What I want is a framework that only contains Swift and that can be "externally linked" to the Objective-C code – Ariel Malka Jan 28 '19 at 20:26
  • I’m not sure what you mean by externally linked, can you please clarify? – l'L'l Jan 29 '19 at 00:52
  • Try [module map](http://nsomar.com/project-and-private-headers-in-a-swift-and-objective-c-framework/), it doesn't require making the Objective-c header public. – kubrick G Jan 29 '19 at 04:47
  • @l'L'l By externally linked I mean: when compiling my framework, I'd like the Objective-C code (think of it as a 3rd party SDK) not to be included. Then later, when creating a new iOS project, I would include my framework (which contains only Swift) and also include the Objective-C code and everything should compile/link fine – Ariel Malka Jan 29 '19 at 13:37
  • @kubrick G I tried module maps, as explained in the link you provided. The nice part is indeed that Objective-C headers are not included in the produced framework. But I'm still stuck, because the produced framework still contains the Objective-C code. As a result, when I include the framework in a new project, which also contains the Objective-C code, I get errors of type "class foo is implemented in both..." – Ariel Malka Jan 29 '19 at 13:47
  • @ArielMalka Because both your Objective-C class are expose to the runtime, whether or not there are in the same module. So the question is why do you want to include the Objective-C Code in your project? If you want to use your Objective-C method in your project, just expose it through swift public interface. – kubrick G Jan 30 '19 at 01:38

1 Answers1

1

If I understand your question correctly, one approach might be to use a system module with a module map. Here is a nice tutorial on wrapping a C library, which should also work for an Objective-C lib: https://www.hackingwithswift.com/articles/87/how-to-wrap-a-c-library-in-swift. Then here is a discussion about using Xcode with SPM: https://forums.swift.org/t/xcode-project-with-spm-dependencies/18157.

Anatoli P
  • 4,791
  • 1
  • 18
  • 22
  • I tried module maps, as explained in the [link provided by Kubrick G](http://nsomar.com/project-and-private-headers-in-a-swift-and-objective-c-framework/). The nice part is indeed that Objective-C headers are not included in the produced framework. But I'm still stuck, because the produced framework still contains the Objective-C code. As a result, when I include the framework in a new project, which also contains the Objective-C code, I get errors of type "class foo is implemented in both..." – Ariel Malka Jan 30 '19 at 08:19