1

I've been tasked with splitting out the model layer of an existing app into a separate framework. I'm trying accomplish this using cocoapods to make a pod which I'll import back into the main app.

The issue I'm having is the app I'm splitting the code out from is a mix of Swift and Objective-c. It's not cleanly split so the Swift code makes use of Objective-c code and the Objective-c code makes use of Swift code.

I'm struggling to understand how to achieve this, or if it's even possible? When you're in a framework the bridging header and the generated *-swift.h don't seem to work the same way. Then pods is another complication on onto.

I've tried to experiment with a simple project here. SPOILER ALERT. It doesn't compile. https://github.com/Megatron1000/TestMixAndMatch

I've tried to find an example of a cocoapod that's setup in the same entangled language way but haven't been able to. Can anyone point me to one I can use as an example?

Mark Bridges
  • 8,228
  • 4
  • 50
  • 65

2 Answers2

1

I have a (private) pod that contains Obj-C and Swift code. In the podspec I've simply added them like this

  s.source_files        = 'Project/*.{swift}',
                          'Project/legacy/*.{h,m}'

Technically in Xcode this project is a framework.

Here you can find the technical details how to combine them in a Project. Technically if you create a framework you'll need to include the Obj-C headers in the umbrella header of the framework If you want to use Swift methods in your Obj-C classes you'll need to include them as

#import <ProductName/ProductModuleName-Swift.h>

if you're building a framework or simply

#import "ProductModuleName-Swift.h"

if you're working on a regular project

Hons
  • 3,804
  • 3
  • 32
  • 50
  • With this setup does your Swift code within the pod make use of the Objective-c code within the pod and does the Objective-c code make use of the Swift code? In your app are you able to access classes in the pod written in both Swift and Objective-c? – Mark Bridges Dec 15 '17 at 10:54
  • I'm still struggling with this. Within the pod itself, I want to import the *-Swift.h file in my Objective-C class to access some of the Swift code. I'm not sure how I get that file? As far as I can see that file isn't being generated. – Mark Bridges Dec 19 '17 at 13:46
  • 1
    Just a very very simple project I've created to show a solution. I've to leave now, but I'll try to extend it a bit further if needed https://github.com/hons82/MixAndMatch – Hons Dec 19 '17 at 15:31
  • Thanks. I've managed to make this work now. I had to go through your project in detail to figure out what was different. My first issue was I'd not made my swift class public and this prevented it from showing up in the -Swift.h file. Then the second issue I was having is it doesn't appear to be possible to import the #import into the .h file. It works fine if it's in the .m file. – Mark Bridges Dec 20 '17 at 13:09
-4

Perhaps the best approach in this case is to simplify the design of the framework and just rewrite objective-c legacy code info swift. You mention that the codebase is already partially in Swift, which will make it even easier to complete the transition.