如何通过 Swift 包管理器在 Xcode 中安装包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34323239/
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 08:19:54  来源:igfitidea点击:

How to Install Package in Xcode via Swift Package Manager

swiftxcodeswift-package-manager

提问by m81

I'm working on a project in Xcode and am attempting to install and use the CryptoSwiftpackage via the Swift Package Manager.

我正在 Xcode 中处理一个项目,并尝试通过 Swift 包管理器安装和使用CryptoSwift包。

I read the documentationon SPM, but I don't see instructions on how to invoke the SPM through Xcode.

我阅读了有关 SPM的文档,但没有看到有关如何通过 Xcode 调用 SPM 的说明。

The examples often refer to calling $ swift build. Does this mean that the SPM is only accessible from the command line? And if so, where exactly am I supposed to create the Package.swiftfile?

这些示例经常提到调用$ swift build. 这是否意味着只能从命令行访问 SPM?如果是这样,我应该在哪里创建Package.swift文件?

I'm reasonably familiar with Xcode, but I don't really understand the meaning of the build settings part of a project, or how Swift modules are used.

我对 Xcode 相当熟悉,但我不太了解项目的构建设置部分的含义,或者如何使用 Swift 模块。

EDIT: This question was originally asked for Xcode 7.2, which shipped with Swift 2.1.1. Will gladly accept answers that explain how to do this with current/future versions of Xcode.

编辑:这个问题最初是针对 Swift 2.1.1 附带的 Xcode 7.2 提出的。很乐意接受解释如何使用当前/未来版本的 Xcode 执行此操作的答案。

回答by Franklin Yu

Swift Package Manager now support generating Xcode project with

Swift Package Manager 现在支持生成 Xcode 项目

swift package generate-xcodeproj

as mentioned in this answer, but this only works when developing a library, or a command line tool. Not (yet) for (iOS/OS X) graphical application developer:

本答案所述,但这仅在开发库或命令行工具时有效。不适用于(iOS/OS X)图形应用程序开发人员

Note that at this time the Package Manager has no support for iOS, watchOS, or tvOS platforms.

请注意,此时包管理器不支持 iOS、watchOS 或 tvOS 平台。

Note:

笔记:

  • It might not work well if there is a space in your package name, i.e. change "App Store" to "AppStore".
  • 如果您的软件包名称中有空格,则可能无法正常工作,即将“App Store”更改为“AppStore”。

回答by timbo

At present, to use the Swift Package Manager aka SPM, you need to install the developmentversion of Swift 2.2 provided by Apple here. Xcode 7.2 shipped with Swift 2.1.1 and does not work with the SPM unless Swift 2.2-dev is installed.

目前,要使用Swift Package Manager aka SPM,您需要安装Apple此处提供的Swift 2.2开发版本。Xcode 7.2 随 Swift 2.1.1 一起提供,除非安装了 Swift 2.2-dev,否则不能与 SPM 一起使用。

You can compile with Swift 2.2-dev but you will need to do the build of CryptoSwift on the command line using swift build. When the library and modulemaps are built, you can drag and drop them into Xcode 7.2 using the Finder. SPM puts the required files into a .builddirectory. But Xcode 7.2 needs to be started with the alternate toolchain. This can be done from the command-line using xcrun launch-with-toolchain /Library/Developer/Toolchains/swift-latest.xctoolchainYou should be able to importfrom the modules/libraries built with the SPM.

您可以使用 Swift 2.2-dev 进行编译,但您需要使用swift build. 构建库和模块映射后,您可以使用 Finder 将它们拖放到 Xcode 7.2 中。SPM 将所需的文件放入一个.build目录中。但是 Xcode 7.2 需要使用备用工具链启动。这可以从命令行使用xcrun launch-with-toolchain /Library/Developer/Toolchains/swift-latest.xctoolchain您应该能够import从使用 SPM 构建的模块/库来完成。

Note though, that you cannot submit iOS apps to the Store at the moment that have been built with the 2.2 developmentversion. You can build/run/test things generally without problem, although Playgrounds are not functional and there can be Xcode issues with the debugger.

但请注意,您目前无法将使用 2.2开发版本构建的 iOS 应用程序提交到应用商店。尽管 Playgrounds 无法正常运行并且调试器可能存在 Xcode 问题,但您通常可以毫无问题地构建/运行/测试事物。

回答by possen

June 4th, 2019 update. Swift 5 and SPM 5 has added this support and Xcode 11 integrates with SPM.

2019 年 6 月 4 日更新。Swift 5 和 SPM 5 已添加此支持,并且 Xcode 11 与 SPM 集成。