In this tutorial, you will learn how to manage dependencies using the Swift Package Manager. For this, we will create a small application that uses a predefined package. If you want to look at a more challenging example, you can visit the swift.org documentation.

Goals

  1. Add a dependency to your project
  2. Create a small application that uses the dependency

1. Add a Dependency to Your Project

Tifig already prepared your project for the inclusion of packages. Open the Package.swift file from your existing Hello_Tifig project by double-clicking it in the Project Explorer window. The following was already created for you:

// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "Hello_Tifig",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages which this package depends on.
        .target(
            name: "Hello_Tifig",
            dependencies: []
        )
    ]
)

The library you are going to use is located on GitHub. It defines the following two public functions:

public func sayHello() {
    print("Hello Tifig")
}

public func sayGoodBye() {
    print("Goodbye Tifig")
}

To use these two functions, you have to make some adjustments to your Package.swift file:

  • In the dependencies section, add the following line:
.package(url: "https://github.com/pipeaesac/Hello_Tifig_Library.git", from: "1.0.0"),

This tells the package manager where the dependency is located and which versions are allowed to be used with your application.

  • In the targets section, add "Hello_Tifig_Library" to the already existing but so far empty target dependency list.

Your resulting Package.swift file should look as follows:

// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "Hello_Tifig",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
        .package(url: "https://github.com/pipeaesac/Hello_Tifig_Library.git", from: "1.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages which this package depends on.
        .target(
            name: "Hello_Tifig",
            dependencies: ["Hello_Tifig_Library"]
        )
    ]
)

That is all it takes. The Hello_Tifig_Library is now ready to be used in your application.

2. Create a Small Application That Uses The Dependency

To use the newly included library, replace the existing code in your main.swift file with the following:

import Hello_Tifig_Library

sayHello()
sayGoodBye()

When saving, the dependency will be downloaded and a file called Package.resolved will be created. It contains a description of your resolved dependency:

{
  "object": {
    "pins": [
      {
        "package": "Hello_Tifig_Library",
        "repositoryURL": "https://github.com/pipeaesac/Hello_Tifig_Library.git",
        "state": {
          "branch": null,
          "revision": "980d2afceb985a5598d7bc6116557b75469857f2",
          "version": "1.0.0"
        }
      }
    ]
  },
  "version": 1
}

Now you can go ahead and run your application. After completing all of the above steps, Tifig should look like this:

Finished Project

Summary

You have successfully finished all the tutorials and are now ready to start using Tifig. We hope you will enjoy working on your Swift projects with it.

If you have ideas or wishes for further tutorials, please let us know.