问题
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
Thanks for your help!
回答1:
I confirmed a working solution, here is how it works:
- It has to be iOS 8+ only (of course...)
- Use Xcode to create a Cocoa Touch Framework
- Develop the framework, mix swift code and objc code as you like. Make sure you mark all swift classes you need to expose as
public
. You may need to consult the documentation - Create a podspec file, provide necessary information (see here for how the pod will look like). One thing in particular is you need to include the Umbrella Header (used in your framework to expose objc code to swift) in the
source_files
in your podspec file. Xcode automatically creates an umbrella header for your framework when you create a framework project. If your framework name isNVMDummy
, the header isNVMDummy.h
. - Push the pod and integrate the pod as a framework into your project.
Test your pod lib. I created a demo app, added my pod lib and in my
ViewController.m
, I did the following:@import MyPodLibMixingOjbcAndSwift; // in viewDidLoad // use objc code in MyLib -> OK... // use swift code in MyLib -> OK...
The demo app works, so I conclude it works.
Here's the demo: https://github.com/axl411/TestNVMDummy
回答2:
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 apod install
(as you should have aPodfile
under yourExample..
folder that has yourMixSwiftObjectiveC
pod) - build and you shouldn't have anymore the issue
来源:https://stackoverflow.com/questions/32459678/how-to-mix-swift-and-objective-c-in-a-pod-lib