请教一下,swift包管理怎么用?我创建了一个swift项目,然后在根目录创建了一个Package.swift文件,
代码如下:
import PackageDescription
let package = Package(
name: "Format",
dependencies: [
.Package(url: "https://github.com/marmelroy/Format.git", majorVersion: 1),
]
)
但是这里“ PackageDescription”报错,系统提示没有这个库,使用swift build也无效。
这个是什么问题,请大神解答一下。
PHP中文网2017-04-17 17:53:46
首先我的理解是 SwiftPM (Swift Package Manager) 不是在 Xcode 裡面這樣使用的。而是直接在命令列裡進行的。
官方簡介:
The Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.
大概意思是, 你可以直接使用 SwiftPM 來進行 下載、編譯、鏈接 你的 Swift 代碼和依賴
而在 OS X 上面安裝的 Xcode 裡面目前(Xcode 7.3)是沒有帶上 SwiftPM 這個工具的,所以當你執行 swift build
時會報錯,如下:swift build
时会报错,如下:
$ swift build
error: unable to invoke subcommand: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-build (No such file or directory)
所以,你需要去下载开发版本的 swift。以下为解决方案:
去官方下载 Swift Trunk Development (master) 版本(即 dev 版本, 与 Xcode 使用版本不冲突)。传送门
下载下来应该是 swift-DEVELOPMENT-SNAPSHOT-2016-03-24-a-osx.pkg
$ export PATH=/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin:"${PATH}"
所以,你需要去下載開發版的 swift。以下為解決方案:去官方下載 Swift Trunk Development (master) 版本(即 dev 版本, 與 Xcode 使用版本不衝突)。傳送門
swift-DEVELOPMENT-SNAPSHOT-2016-03-24-a-osx.pkg
這樣一個檔案 大約 200MB
$ swift build --version
Apple Swift Package Manager 0.1
然後檢查是否安裝成功
$ cd ~/Desktop
$ mkdir helloSwiftPM
$ cd helloSwiftPM
使用🎜首先,建立一個空目錄🎜
$ swift build --init
🎜然後,初始化這個目錄。 🎜
$ swift build
🎜編譯🎜
$ .build/debug/helloSwiftPM
Hello, world!
🎜運行🎜
rrreee
🎜到此就 OK 了