10

So I started work on adding Swift to an existing Objective-C framework.

The bad news is that it was rather simple to do, and appeared as if it was working. Meaning that I got the Swift code to see the obj-c classes and even build rather easily. I setup a bridging header added what I needed to it, changed the module name etc, and basically everything was working fine.

Later on I realized a few things.

Firstly, you're not supposed to have a bridging header in a framework. In fact I tried with a test project and it wouldn't let me. But someone in my actual project it allowed it.

It definitely starts cocking things up. I think because it seems to include the bridging header in the Swift generated header or maybe something else with how it imports the umbrella header. I get the simplicity of having the swift automatically use the Umbrella header and nothing else but I need another header for several reasons.

  1. Import private headers from inside the framework that I don't want to be public framework headers.
  2. Import the other frameworks which we are linked to and need access to the public headers of. I don't want to import these other frameworks in the umbrella header of this framework.

There are several problems I face in this scenario.

External targets can't load this framework using @import. And I can't import the framework's bridging header directly either. Meaning that I can't access the Swift code from another target. I wrote an accessor class and in this case it's not a big deal. Until I realized that includes a test-case target, so I can't effectively test this code without some sort of test implementation that would be in the framework target. Not even a swift test in my test target can access the Swift.

So is there any way to do this? TL:DR "Can I have a mixed Swift/Obj-c framework that has its Swift + Obj-c accessibly externally and keep some of its obj-c private?"

Siyual
  • 16,415
  • 8
  • 44
  • 58
napierzaza
  • 439
  • 5
  • 21
  • I'm pretty sure you can, my man, take a look here: http://stackoverflow.com/questions/28815487/building-a-swift-framework-with-references-to-objective-c-code – Larry Pickles Nov 30 '15 at 19:07
  • Looks like an interesting situation. Please keep us posted on how it goes or if you run into any other problems. The other question referenced in the previous comment deals with using an Obj-C framework in Swift, but based on the Apple docs mentioned there it is possible to write a framework in a mix of Obj-C and Swift. – Anatoli P Dec 01 '15 at 04:18
  • I think, there's no solution to this problem, yet. Interoperability seems to only work via public objc-headers or public swift-types. That makes mixed-frameworks and slow & steady migration of frameworks to Swift useless to me, as a key-feature of frameworks is to only expose a certain API and keep most of it private. If an expert could clarify as to why it's like that, I'd appreciate that and accept it as an answer. – Felix Lieb Jan 29 '16 at 14:13
  • @napierzaza: Have a look at this: http://stackoverflow.com/a/24005242/1208191 He says `In some situations, particularly when working with ObjC frameworks, you don't add an Objective-C class explicitly and Xcode can't find the linker. In this case, create your .h file named as mentioned above, then make sure you link its path in your target's project settings like so` Mine framework DOES work with bridging header!!! In fact it does not compile otherwise, however, I am stuck at importing this framework (project) in other app target. Mine says "Failed to import bridging header ***". – Sunil Chauhan Oct 07 '16 at 20:50

1 Answers1

0

I solved it.

I add public to class and protocol, so that I can access swift code in objective-c

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Zev003
  • 85
  • 4