19

I have created a new swift lib pod using the command : pod lib create MixSwiftObjectiveC found from https://guides.cocoapods.org/making/using-pod-lib-create.html

In this pod lib I need to use a code that's written in Objective-C. If I would have a separate "normal" project I would just include the import in the header file …-Bridging-Header.h. Is there an equivalent for a pod lib? Can Swift and Objective-C be mixed inside a pod lib?

As an example I've created this github: https://github.com/crarau/MixSwiftObjectiveC In order to run the example you'll need XCode Version 7.0 beta 6 and Swift 2.0

Compilation issue

Thanks for your help!

Ciprian Rarau
  • 3,040
  • 1
  • 30
  • 27

2 Answers2

8

This is actually pretty straight forward in cocoapod. you just need to define these lines in the .podspec file.

s.source_files = '{YOUR_POD}/Classes/**/*.{swift,h,m}'
s.public_header_files = '{YOUR_POD}/Classes/**/*.h'

"s.public_header_files" automatically create an umbrella header that serves the purpose of the bridging header as well. That's it you should be able to use ObjC classes inside swift classes.

You can also use swift classes in ObjC. you just need to import -swift header in your ObjC file.

#import <YOUR_POD/YOUR_POD-Swift.h>

As long as the dependency is one way, you would not face any issues.

abhinavroy23
  • 3,630
  • 1
  • 14
  • 19
  • Notice that you can't include swift headers in the regular headers this way, but you can do it in your implementation files. See https://stackoverflow.com/a/28484941/674557 – apollov Jan 13 '23 at 19:33
3

In your .podspec you should have something like:

s.source_files = 'MixSwiftObjectiveC/Classes/*'

  • Go to the MixSwiftObjectiveC/Classes/ directory folder in the finder to be sure and put your files (objc and swift)
  • Clean Build Folder
  • navigate to your Example.. folder and do a pod install (as you should have a Podfile under your Example.. folder that has your MixSwiftObjectiveC pod)
  • build and you shouldn't have anymore the issue
denis_lor
  • 6,212
  • 4
  • 31
  • 55