muthu
muthu

Reputation: 89

How to add dependency using Swift package manager

I am using xcode version 10.1 and swift 4 I want to add a new dependency https://github.com/IBM-Swift/BlueECC/blob/master/README.md to my existing project I am following the below steps to install using swift package manager

  1. Navigate to the root folder in terminal
  2. swift package init
  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", dependencies: ["CryptorECC"]) In respective place
  4. In terminal give swift build
  5. Import the package

But the package is not getting imported, please correct me where I am making mistake

Upvotes: 5

Views: 4058

Answers (3)

Soumya Mahunt
Soumya Mahunt

Reputation: 2802

  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")
    

    In the above snippet I have provided an example of version requirements, you can also have branch and commit requirements.

  2. Next, to use modules from 3rd party dependencies you have to specify the product name the module is included in and the package name in the target dependency:

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

    You can see what product contains what modules in the products section of package manifest of 3rd party libraries:

    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"]
        )
    ],
    

Upvotes: 0

yo2bh
yo2bh

Reputation: 1436

There are four ways to add the dependencies in Package.swift manifest file. Adding Alamofire library as example.

Using tag version

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

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

Using branch name

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

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

Using commit id

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

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

Using local repository

// 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"]),

Please find more techanical details about Swift PM here.

Upvotes: 14

curiously77
curiously77

Reputation: 233

You will need the package URL, version number and the name of the package.

Add this to your manifest:

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

After that add this to your target file:

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

Upvotes: 0

Related Questions