SPM: Swift Package Manager

Safia CHMITI
6 min readFeb 17, 2021

If you are here, that means you want to discover what Swift Package Manager a.k.a SPM is. If that’s the case, let’s go 🚀 🚀 !!!!

In this article, you will first learn what SPM is, how to create it, and finally how to add local and remote dependencies in your package ✌🏻.

SPM: Swift Package Manager

Before showing you how to use SPM in your project, it’s important to know first of all what it is 🤔 .

Let’s see what Apple Documentation says about it 👀: Create reusable code, organize it in a lightweight way, and share it across Xcode projects and with other developers.

You certainly know Cocoapods or Carthage which allow you to add dependencies in your project. We can say that SPM belongs to the same family. It is a dependency manager which was introduced along with swift 3 in 2015. But 2019 also includes great news (despite Coronavirus 🦠) . 2019 comes with the release of Xcode 11 where we can access SPM directly from Xcode. It’s a great announcement 🥳 in WWDC 2019, as it will facilitate the daily life of an iOS developer.

PACKAGE CREATION

There are two ways to create a package. The first one is from the terminal and the second one is from Xcode.

Terminal:

To create an SPM through Terminal, you only have to use those commands:

mkdir MyPackage

cd MyPackage/

swift package init

Congratulations 🎊 , by running the last command, you have successfully created your first package that contains basic files of an SPM. Don’t mind the content right now, we will treat it more in depth in the upcoming section.

To open your package in Xcode. You only have to open one of the .swift files.

Xcode:

Now let’s see how we can create a package from Xcode when you already have a created project.

There are two possibilities. The first one is File > New > Swift Package. The second one is for keyboard shortcuts lovers ⌨️: Shift + Control + Command + N .

Make sure that the package is created within the project folder, that you’ve added it to the project and that the current group is the project.

Great 👌🏼, now that you have created your package, let’s discover its content together.

PACKAGE CONTENT

When you open your package in Xcode, you’ll get this:

The SPM is composed of a manifest file which is a Package.swift file in order to manage its dependencies, Sources folder that will contain build targets and Tests folder for test targets.

Let’s see what Package.swift contains and how we can add dependencies. Let’s go!

Open your Package.swift file.

You’ll find it in the root directory of the package which will include:

  • 1️⃣: a Swift tools version: It indicates the minimum version of Swift the compiler needs to parse the package.

⚠️ It should be the first line of the file as a comment and you do not have to remove it. It helps the package understand how to interpret it.

  • 2️⃣: Here we import the PackageDescription library. It enables you to describe your package.
  • 3️⃣: We instantiate a package to configure the package using products, dependencies and targets in the example’s name. The package has a “MyPackage” name.
  • 4️⃣: It is simply the products that the package will provide afterwards. It defines what other applications and libraries can import.
  • 5️⃣: Here we declare the packages that the package will depend on. They can be remote or local and sometimes both.
  • 6️⃣: Package targets. In this case, we had the first one that was dedicated to the library, and the second one to tests.

ADDING DEPENDENCIES

We can add two types of dependencies to the spm: local and remote. First, I’ll show you how to add a remote one and after that the local one.

Remote:

To add a remote dependency, you’ll need:

  • Url: It’s the url that you use when you want to clone a github repository with https.
  • The package version: you’ll also find it in the github repository.
  • The name of the dependency: You’ll find it in the package.swift of the dependency.

Now, let’s see an example. I chose SDWebImage Library. It is a library that provides an async image downloader with cache support.

So here I used the url which is “https://github.com/SDWebImage/SDWebImage.git”, I specified the version and finally used the name in our build target. For instance, if you want to use a dependency for your unit tests, be sure to add the name in the test target and not in the build.

In my example, I used a property “from” so as to fetch dependency that will at least start from 5.10.0. Notice that I specified 5.10.0 in the manifest file, but the fetched version is 5.10.4. Magical!

There are other properties that you can specify. It depends on your need. Here is a list:

  • .branch: if you are looking for specific branch version

package(url: “https://github.com/SDWebImage/SDWebImage.git", .branch(“5.1.x”))

  • .revision: if you are looking for a specific commit

.package(url: “https://github.com/SDWebImage/SDWebImage.git", .revision(“a6b6e44eadf0d39250c10a7cc0e3b91d0bdb0e94”))

  • .exact: if you want to have 5.10.0 exactly. For example:

.package(url: “https://github.com/SDWebImage/SDWebImage.git", .exact(“5.10.0”))

  • a range: if you want to set a minimum and maximum for your package version.

.package(url: “https://github.com/SDWebImage/SDWebImage.git", “5.10.0”…”5.10.2")

That’s all for adding a remote dependency. Let’s see how local dependencies work.

Local:

We can also add an spm but locally. It can be useful for the team and maybe others iOS teams, because you only have to add the package without specifying its properties as a remote one. In general, there won’t be issues about versioning and so on.

To add a local dependency to your project, you have to:

  • Add the package folder path in the manifest file.

CONCLUSION

That’s all for using SPM in your Xcode project. I hope this article was useful and that you know more about SPM now :). Thank you for reading !!

--

--