如何在其他项目中重用Swift代码? [英] How to reuse Swift code in other projects?

查看:122
本文介绍了如何在其他项目中重用Swift代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Swift写了一个类。我想在我写的两个独立的iOS应用程序项目中使用此代码。共享代码和应用程​​序都是用Swift编写的。这样做的最佳方式是什么?

I wrote a class in Swift. I want to use this code in two separate iOS app projects that I wrote. Both the shared code and the apps are written in Swift. What is the best way of doing that?

我试图在Swift中创建框架和库,然后将其作为子项目添加到我的应用程序中。在这两种情况下,我都无法让应用程序看到模块。我尝试将共享模块添加到主应用程序目标的Target Dependencies和Link Binary With Libraries。没有运气 - 应用程序仍无法看到共享模块的类。

I tried to create both a framework and a library in Swift and then add it as a sub-project to my app. In both cases I could not make the app see the module. I tried to add the shared module to "Target Dependencies" and "Link Binary With Libraries" of the main app's target. No luck - the app still can not see the classes of the shared module.

目前使用Xcode6 Beta6。

Using Xcode6 Beta6 at the moment.

正如Konstantin Koval和Heliem所指出的,我所需要的就是在课堂上使用 public 和共享模块中的方法声明。现在一切正常,如果我使用工作区,如果我将模块添加为子项目。

As Konstantin Koval and Heliem pointed out, all I needed is to use public in my class and method declarations, in the shared module. Now all works, both if I use workspace and if I add the module as a subproject.

我刚刚找到了一个很好的简单解决方案,可以在Swift中重用项目之间的代码。它被称为Carthage( https://github.com/Carthage/Carthage )。这不是插件,因为我不以任何方式与它有任何联系,我真的很喜欢它。它适用于iOS 8 +。

I just found an excellent easy solution for reusing code between projects in Swift. It is called Carthage (https://github.com/Carthage/Carthage). This is not a plug as I am not affiliated with it in any way, I just really like it. It works in iOS 8+.

推荐答案


  1. 创建一个新项目(iOS Cocoa Touch Framework)您的可重用代码

  2. 将您的课程移至该框架项目

  3. 标记您的方法和类,其他人应该可以看到 public

创建工作区。

您可以在步骤1中创建工作区。在创建新的Framework项目时,Xcode会询问您是否要创建新工作区并将该项目添加到工作区。这是最好的方法

Create Workspace.
You can create a workspace on step 1. When you create new Framework project, Xcode will ask you if you want to create new workspace and add that project to workspace. This is the best approach

这篇关于如何在其他项目中重用Swift代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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