在现有的 xcode 项目上使用 swift 包管理器

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

Use swift package manager on existing xcode project

iosswiftxcodeswift-package-manager

提问by jesuscc29

im new to the swift and xcode world, so i'm having a problem trying to integrate a package to my project.

我是 swift 和 xcode 世界的新手,所以我在尝试将包集成到我的项目时遇到了问题。

I want to add Alamofire dependency, with the following commands:

我想使用以下命令添加 Alamofire 依赖项:

Inside my root project folder:

在我的根项目文件夹中:

swift init

this creates the Package.swift file, i add the dependency inside, run then:

这将创建 Package.swift 文件,我在里面添加依赖项,然后运行:

swift build

Everything seems to be ok, but im my project when i try to import my library:

一切似乎都很好,但是当我尝试导入我的库时,我的项目是:

import Alamofire

I get an error, it says that the module is not recognized. So my question here is, what is the correct steps to integrate Package Manager and a dependency on a existing project without crashing everything.

我收到一个错误,它说模块未被识别。所以我的问题是,在不使一切崩溃的情况下,集成包管理器和对现有项目的依赖的正确步骤是什么。

UPDATE:

更新:

swift build

outputs:

输出:

Resolved version: 4.3.0
Compile Swift Module 'Alamofire' (17 sources)
Compile Swift Module 'Sample' (1 sources)

And my Package.swift is:

我的 Package.swift 是:

import PackageDescription

let package = Package(
    name: "Sample",
    dependencies: [
        .Package(url: "https://github.com/Alamofire/Alamofire.git", majorVersion: 4)
    ]
)

采纳答案by Eneko Alonso

Swift Package Manageris a standalone tool which allows managing dependencies and building projects without Xcode. It can generate Xcode projects for you with swift package generate-xcodeproj.

Swift Package Manager是一个独立的工具,它允许在没有 Xcode 的情况下管理依赖项和构建项目。它可以为您生成 Xcode 项目swift package generate-xcodeproj

However, at the moment, Swift Package Manager only has support for building projects for macOS and Linux platforms. The only way to build projects for iOS, tvOS and watchOS is using Xcode, which includes the SDKs needed for these platforms.

但是,目前 Swift Package Manager 仅支持为 macOS 和 Linux 平台构建项目。为 iOS、tvOS 和 watchOS 构建项目的唯一方法是使用 Xcode,其中包括这些平台所需的 SDK。

There are ways to use Swift Packages Manager to manage dependencies for iOS/tvOS/watchOS, but it is not easy and requires manual work. If you are interested, take a look at https://github.com/j-channings/swift-package-manager-ios

有一些方法可以使用 Swift Packages Manager 来管理 iOS/tvOS/watchOS 的依赖项,但这并不容易,需要手动操作。如果你有兴趣,可以看看https://github.com/j-channings/swift-package-manager-ios

Other than that, I'd recommend using Carthageor CocoaPods.

除此之外,我建议使用CarthageCocoaPods

Update for Xcode 11

Xcode 11 更新

Swift Package Manager is now integrated into Xcode 11. You can add your package by going to "File" then "Swift Packages" then "Add Package Dependency..." Paste the repository's URL into the field above then click "next". Xcode will walk you through the rest of the steps. You can learn more at this WWDC talk.

Swift 包管理器现已集成到 Xcode 11 中。您可以通过依次转到“文件”、“Swift 包”和“添加包依赖项...”来添加包。将存储库的 URL 粘贴到上面的字段中,然后单击“下一步”。Xcode 将引导您完成其余步骤。您可以在此 WWDC 演讲中了解更多信息。