ios - swift 包管理怎么用
大家讲道理
大家讲道理 2017-04-17 17:51:46
0
2
365

请教一下,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也无效。

这个是什么问题,请大神解答一下。

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回覆(2)
Ty80

首先我的理解是 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。以下为解决方案:

安装

  1. 去官方下载 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。以下為解決方案:
    安裝
    1. 去官方下載 Swift Trunk Development (master) 版本(即 dev 版本, 與 Xcode 使用版本不衝突)。傳送門

      下載下來應該是 swift-DEVELOPMENT-SNAPSHOT-2016-03-24-a-osx.pkg 這樣一個檔案 大約 200MB

      將最新的 Swift 版本配置到環境變數裡。開啟命令列執行

      $ 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 了
    PHPzhong

    swift package generate-xcodeproj 先生成xcode的工程文件。然後打開就可以了。

    熱門教學
    更多>
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板