xcode 如何在其他项目中重用 Swift 代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25518077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:26:33  来源:igfitidea点击:

How to reuse Swift code in other projects?

iosxcodeswift

提问by Evgenii

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 写了一个类。我想在我编写的两个单独的 iOS 应用程序项目中使用此代码。共享代码和应用程​​序都是用 Swift 编写的。这样做的最佳方法是什么?

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.

我尝试在 Swift 中创建一个框架和一个库,然后将它作为子项目添加到我的应用程序中。在这两种情况下,我都无法让应用程序看到模块。我尝试将共享模块添加到主应用程序目标的“目标依赖项”和“链接二进制库”。运气不好 - 应用程序仍然看不到共享模块的类。

Using Xcode6 Beta6 at the moment.

目前使用 Xcode6 Beta6。

Solution

解决方案

As Konstantin Koval and Heliem pointed out, all I needed is to use publicin 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.

正如 Konstantin Koval 和 Heliem 指出的那样,我所需要的只是public在我的类和方法声明中使用,在共享模块中。现在一切正常,无论是使用工作区还是将模块添加为子项目。

Update

更新

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+.

我刚刚找到了一个极好的简单解决方案,用于在 Swift 中的项目之间重用代码。它被称为迦太基 ( https://github.com/Carthage/Carthage)。这不是插件,因为我与它没有任何关联,我只是非常喜欢它。它适用于 iOS 8+。

回答by Kostiantyn Koval

  1. Create a new project (iOS Cocoa Touch Framework) for your reusable code
  2. Move your classes to that Framework project
  3. Mark your methods and classes, that should be visible to others as public
  4. 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
  5. Add both your project and Framework to the workspace
  6. Select you project target -> General tab. Add Framework and Libraries (add your library here)
  7. When you want to use code from your Library in swift file, import it using import 'LibTargetName'
  1. 为您的可重用代码创建一个新项目(iOS Cocoa Touch Framework)
  2. 将您的类移至该框架项目
  3. 将您的方法和类标记为其他人应该可见的 public
  4. 创建工作区。
    您可以在步骤 1 中创建一个工作区。当您创建新的 Framework 项目时,Xcode 会询问您是否要创建新的工作区并将该项目添加到工作区。这是最好的方法
  5. 将您的项目和框架添加到工作区
  6. 选择您的项目目标 -> 常规选项卡。添加框架和库(在此处添加您的库)
  7. 当您想在 swift 文件中使用库中的代码时,请使用 import 'LibTargetName'

回答by J. K.

You can take a more programatic approach by using SWM(Swift Modules): https://github.com/jankuca/swm

您可以通过使用SWM(Swift 模块)采取更具程序性的方法:https: //github.com/jankuca/swm

It is very similar to npm(node.js package manager) or bower(client-side module manager); you declare your dependencies in a swiftmodule.jsonfile like below. It does not have a central module registry like the two mentioned JS solutions, it only accepts git URLs.

它与npm(node.js 包管理器)或bower(客户端模块管理器)非常相似;您在swiftmodule.json如下文件中声明您的依赖项。它没有像上面提到的两个 JS 解决方案那样的中央模块注册表,它只接受 git URL。

{
  "name": "ProjectName",
  "dependencies": {
    "Dependency": "git://github.com/…/….git"
  }
}

…run swm installand have the Dependencymodule (compiled into a *.swiftmodulebinary) ready for import in the .modules/directory.

...运行swm install并准备好将Dependency模块(编译为*.swiftmodule二进制文件)导入.modules/目录中。

import Dependency

And if you prefer to skip Xcode for app development, you can also build your whole app using swm build(more info in the project's readme).

如果您更喜欢跳过 Xcode 进行应用程序开发,您还可以使用swm build(项目自述文件中的更多信息)构建整个应用程序。

The project is still in early stages but it makes itself useful a lot for me at least. It provides the most clean (and clear) way of creating importable modules.

该项目仍处于早期阶段,但至少它对我来说很有用。它提供了创建可导入模块的最干净(和清晰)的方式。

回答by Tyler Long

Here is a video which is very straightforward: http://eonil-observatory.tumblr.com/post/117205738262/a-proper-way-to-add-a-subproject-to-another-xcode

这是一个非常简单的视频:http: //eonil-observatory.tumblr.com/post/117205738262/a-proper-way-to-add-a-subproject-to-another-xcode

The video is for OS X instead of iOS. But you will get it and figure out the process for iOS.

该视频适用于 OS X 而不是 iOS。但是你会得到它并弄清楚iOS的过程。

Let's assume that AppAneeds to reused code from SharedProject.

让我们假设AppA需要重用来自SharedProject.

The following process works for me in Xcode 7 Beta:

以下过程适用于 Xcode 7 Beta:

  1. Create SharedProject. Project type must be Frameworkinstead of Application. You write common code in this project and mark the code as public.
  2. Open AppAin Xcode, open the folder which contains SharedProjectin Finder. Drag the .xcodeproj file of SharedProjectfrom Finder and drop it into the root folder of AppAin Xcode Project Navigator.
  3. AppA--> Build Phases--> Link Binary with Libraries. Add SharedProject.
  4. import SharedProjectand reuse code from SharedProject!
  1. 创建SharedProject. 项目类型必须是Framework而不是Application. 您在此项目中编写通用代码并将代码标记为public.
  2. 打开AppA在Xcode中,打开包含的文件夹SharedProject中查找。将SharedProjectFinder 中的 .xcodeproj 文件拖放到AppAXcode Project Navigator的根文件夹中。
  3. AppA--> Build Phases--> Link Binary with Libraries. 添加SharedProject.
  4. import SharedProject并重用代码SharedProject

Edit:

编辑:

Nowadays I suggest you use Carthage. It's better than the home made solution above.

现在我建议你使用Carthage。它比上面自制的解决方案更好。