An Introduction to Swift Package Manager and how it works

prateek arora
YML Innovation Lab
Published in
6 min readApr 11, 2023

What is a package Manager?

A package manager or package-management system is a collection of software tools that automates the process of installing, upgrading, configuring, and removing computer programs for a computer in a consistent manner.

or

A package manager deals with packages, distributions of software and data in archive files. Packages contain metadata, such as the software’s name, description of its purpose, version number, vendor, checksum (preferably a cryptographic hash function), and a list of dependencies necessary for the software to run properly.

What is Swift Package Manager (SPM)

As per official Apple Swift Documentation:

“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.”

The Package Manager is included in Swift 3.0 and above.

Why do we need Swift Package Manager?

Though we have multiple other ways to manage the dependencies like cocoa pods, Carthage etc. then why is there a need to use SPM?

The answer is both cocoa pods and Carthage are third-party package managers, which eventually leads to extra code and efforts one has to put in order to utilise these third-party package managers.

For example: For cocoa pods, we need to have a pod framework to be installed, which then needs ruby, gem file or brew in order to install pods.

But then SPM comes inbuilt with XCode, so there won’t be extra effort and extra files that will be getting added once we use SPM.

System Requirement

  1. SwiftPM is compatible with macOS, iOS, tvOS and Linux
  2. For macOS, you need XCode 11 and above.
  3. For Linux, you have to install it manually or use swiftenv.

Advantages

  1. SwiftPM is open-source like Cocoapods. It may be challenging or fast to contribute but it is possible.
  2. Ease of managing dependency’s dependencies. If a dependency relies on another dependency, the Swift Package manager will handle it for you.
  3. Ease of resetting, managing, and downloading the latest package version.
  4. There is no need to update the SPM externally as with Carthage and cocoa pods as it’s a part of XCode itself.

Limitations

  1. There is no support for build phases and build settings.
  2. No support for the “.xcodeproj” file.
  3. Won’t be able to deploy the package directly over the device or simulator, the package has to be part of the project.
  4. Swift Linting is tedious and not straightforward, which is explained below.

Can we achieve Swift Linting in SPM?

The answer is you can and you can’t at the same time.

The Swift Package Manager (SPM) “integrated” in Xcode can be used for “embedding code” into your app. Not to embed an executable (like Swiftlint).

So, the solution is to create a “parallel” project with SPM alongside your app.

It’s described in the following link: It’s time to use Swift Package Manager

So the idea is to have a Package.swift with Swiftlint, build/run it and it should launch then Swiftlint
The first time, it will fetch the code, build and then run. So it might take some time.

It’s also the solution given by SwiftFormat in their ReadMe.

Without further ado, let’s create our package

Create an XCode project. Once you have created an XCode let’s create a new Swift Package.

Step 1: Open Xcode installed in your macOS, and click on “Create a new Xcode project”, which will then open the next window.

Step 2: Choose the template as Multiplatform from the list of templates given in the top bar then select “Swift package”. under framework and library.

Step 3: Click next after the template selection and give your package name and where you want to save, in our case package name is “MySwiftPackage” which we are storing on our desktop, you can choose the name of the package and location accordingly.

Step 4: On click of “Create” as referred to in the above screenshot, a new package will get created with a folder structure similar to the below screenshot.

Following are the major files that got added, once we created the swift package i.e

  1. Readme.md : The file which contains a description of the package.
  2. Package.swift : Contains all the required configuration of your package i.e external dependencies, targets etc.

Step 5: Add some boilerplate code in our package swift file.

Step 6: As a next step, we’ll push this code into some remote repository, such that it can then be utilized in our test application or whatever application you want. For this sample package, I have pushed it into Git Hub.

Now we have successfully created our swift package, the next steps would be how to use this package in our test application.

How to use the Swift package in our test application?

We will follow the below steps in order to use the swift package in our test application.

Step 1: We have to get the Git hub remote repository path where we have pushed our package, please refer below screenshot.

Step 2: Create a sample test application where you want to use this package. In our case, it is “MyTestApp”

Step 3: Click on “Package Dependencies” such that you will see a view like this as shown in the below screenshot.

Step 4: Click on the “+” button so as to add the package we created before.

Step 5: On click of the “+” button package search window will get opened.

Step 6: Now you can search the package by putting the copied Git Hub URL (Refer to step 1) in the search box.

Step 7: Next on clicking on the “Add Package” button the following window will get open.

Step 8: Again on click of “Add Package”, your swift package will get added successfully in your application.

Step 8: Before using your swift package code in any of your ViewController or any other swift file, make sure the swift package which you added is properly linked under “Link Binary with Libraries”

Hurray !! , Now we are all set to use our swift package code.

Useful Links:

  1. https://www.swift.org/package-manager/
  2. https://developer.apple.com/documentation/xcode/adding-package-dependencies-to-your-app
  3. https://fig.io/manual/swift/package

Thanks Mark Pospesel, Sanath Rai and Srividya Menon for your review.

--

--