如何在Pod lib中混合Swift和Objective-C? [英] How to mix Swift and Objective-C in a pod lib?

查看:294
本文介绍了如何在Pod lib中混合Swift和Objective-C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下命令创建了一个新的swift lib pod:pod lib create MixSwiftObjectiveC可从 https://guides.cocoapods.org/making/using-pod-lib-create.html

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

在这个pod库中,我需要使用用Objective-C编写的代码. 如果我有一个单独的普通"项目,则只需将导入内容包括在头文件…-Bridging-Header.h中. 豆荚库有对应的内容吗? Swift和Objective-C可以在pod lib中混合吗?

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?

作为示例,我创建了这个github: https://github.com/crarau/MixSwiftObjectiveC 为了运行该示例,您将需要XCode Version 7.0 beta 6和Swift 2.0

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

感谢您的帮助!

推荐答案

我确认了一个可行的解决方案,它是这样工作的:

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

  1. 它只能是iOS 8以上版本(当然...)
  2. 使用Xcode创建可可触摸框架
  3. 开发框架,根据需要混合使用swift代码和objc代码.确保将需要公开的所有swift类标记为public.您可能需要查阅此处) .特别要注意的一件事是,您需要在podspec文件的source_files中包括Umbrella Header(在框架中使用,以使objc代码快速显示).创建框架项目时,Xcode会自动为框架创建一个伞形标头.如果您的框架名称为NVMDummy,则标头为NVMDummy.h.
  4. 推送pod并将pod作为框架集成到您的项目中.
  5. 测试您的pod库.我创建了一个演示应用程序,添加了pod lib,然后在ViewController.m中执行以下操作:

  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.

这是演示: https://github.com/axl411/TestNVMDummy

这篇关于如何在Pod lib中混合Swift和Objective-C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆