How to mix Swift and Objective-C in a pod lib?

非 Y 不嫁゛ 提交于 2019-12-03 03:28:37
axl411

I confirmed a working solution, here is how it works:

  1. It has to be iOS 8+ only (of course...)
  2. Use Xcode to create a Cocoa Touch Framework
  3. 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
  4. 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 is NVMDummy, the header is NVMDummy.h.
  5. Push the pod and integrate the pod as a framework into your project.
  6. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!