问题描述
我是swift和xcode领域的新手,所以尝试将软件包集成到我的项目时遇到了问题.
im new to the swift and xcode world, so i'm having a problem trying to integrate a package to my project.
我想使用以下命令添加Alamofire依赖项:
I want to add Alamofire dependency, with the following commands:
在我的根项目文件夹中:
Inside my root project folder:
swift init
这将创建Package.swift文件,我在其中添加依赖项,然后运行:
this creates the Package.swift file, i add the dependency inside, run then:
swift build
一切似乎都还可以,但是当我尝试导入我的库时,我是我的项目:
Everything seems to be ok, but im my project when i try to import my library:
import Alamofire
我得到一个错误,它表明该模块未被识别.所以我的问题是,在不使所有程序崩溃的情况下,集成Package Manager和对现有项目的依赖关系的正确步骤是什么?
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.
更新:
swift build
输出:
Resolved version: 4.3.0
Compile Swift Module 'Alamofire' (17 sources)
Compile Swift Module 'Sample' (1 sources)
我的Package.swift是:
And my Package.swift is:
import PackageDescription
let package = Package(
name: "Sample",
dependencies: [
.Package(url: "https://github.com/Alamofire/Alamofire.git", majorVersion: 4)
]
)
推荐答案
Swift Package Manager 是独立工具,无需Xcode即可管理依赖关系和构建项目.它可以使用 swift包generate-xcodeproj
为您生成Xcode项目.
Swift Package Manager is 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仅支持为macOS和Linux平台构建项目.构建适用于iOS,tvOS和watchOS的项目的唯一方法是使用Xcode,其中包括这些平台所需的SDK.
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 Packages Manager来管理iOS/tvOS/watchOS的依赖关系,但这并不容易,并且需要手动操作.如果您有兴趣,请查看 https://github.com/j-channings/swift-package-manager-ios
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
Other than that, I'd recommend using Carthage or CocoaPods.
为Xcode 11更新
Swift软件包管理器现在已集成到Xcode 11中.您可以通过以下方式添加软件包:文件",然后依次单击"Swift软件包"和添加软件包依赖项...",将存储库的URL粘贴到上面的字段中,然后单击下一步".".Xcode将引导您完成其余的步骤.您可以在此WWDC演讲中了解更多信息.
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.
这篇关于在现有的xcode项目上使用Swift Package Manager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!