簡體   English   中英

如何使用 Swift package 管理器添加依賴項

[英]How to add dependency using Swift package manager

我正在使用 xcode 版本 10.1 和 swift 4 我想添加一個新的依賴項 https://github.com/theIBM-Swift/BlueECC/b使用以下現有項目安裝到 mymaster/theREADME 步驟。 swift package 經理

  1. 導航到終端中的根文件夾
  2. swift package 初始化
  3. Open package.swift file in folder and add the.package(url: " https://github.com/IBM-Swift/BlueECC.git ", from: "1.2.4") and.target(name: "example" , 依賴項: ["CryptorECC"]) 在各自的地方
  4. 在終端中給出 swift 構建
  5. 導入 package

但是 package 沒有被導入,請糾正我的錯誤

有四種方法可以在 Package.swift 清單文件中添加依賴項。 添加 Alamofire 庫作為示例。

使用標簽版本

 // Syntax
.package(url: "Repository URL", from: "Version") 

// Example
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.0.2") 

使用分支名稱

// Syntax
.package(url: "Repository URL", .branch("branch-name"))

// Example
.package(url: "https://github.com/Alamofire/Alamofire.git", .branch("master"))

使用提交 ID

 // Syntax
 .package(url: "Repository URL",
                  .revision("commit-id"))

 // Example
 .package(url: "https://github.com/Alamofire/Alamofire.git",
                  .revision("eb67560abbc58e7d98e6e17daa5bd9376625f038"))

使用本地存儲庫

// Syntax
.package(path: "../Repository local path")

// Example
.package(path: "../Alamofire")

After that add this to your target section in Package.swift file: This is mandatory to add as this will make the module accessible to Swift PM, otherwise library/module will not accessible in Swift PM.

// Syntax
.target(
    name: "target-name",
    dependencies: ["dependency-name"]),

// Example
.target(
    name: "MyApp",
    dependencies: ["Alamofire"]),

在此處找到有關 Swift PM 的更多技術細節。

您將需要 package URL、版本號和 package 的名稱。

將此添加到您的清單中:

.package(url: 'the url of the package', from: 'version number')

之后將其添加到您的目標文件中:

.target(
    name: "target-name",
    dependencies: ["dependency-name"]),
  1. To add any third party swift package as a dependency in your swift package first you have to add the package url and requirements to the package dependency:

     .package(url: "https://github.com/IBM- Swift/BlueECC.git", from: "1.2.4")

    在上面的片段中,我提供了一個版本要求的示例,您還可以有分支和提交要求。

  2. 接下來,要使用來自 3rd 方依賴項的模塊,您必須在目標依賴項中指定包含該模塊的產品名稱和 package 名稱:

     .product(name: "CryptorECC", package: "BlueECC")

    您可以在 3rd 方庫的 package 清單的產品部分中查看哪些產品包含哪些模塊:

     products: [ // Products define the executables and libraries produced by a package, // and make them visible to other packages. .library( // the product name name: "CryptorECC", // included modules in product targets: ["CryptorECC"] ) ],

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM