At this years WWDC Apple announced that Xcode 11 will contain support for using Swift packages through the Swift Package Manager. This is great news for developers of Apple platforms as it will allow us to create, publish, add, remove and manage dependencies straight from Xcode. Let us have a look at how to work with this exciting new feature.

What is a Swift Package?

Apple describes a Swift Package as:

A set of reusable components of Swift, Objective-C, Objective-C++, C or C++ code that developers can use in their projects.

This could be code that you have written yourself, for example, a networking library that you share among multiple personal projects or projects within the organization you work for. But it could also be someone else’s code, for example, an open source library like Alamofire.

A Package is a directory that follows a clear structure. It contains the following files:

Package.swift

The top level of the directory contains the manifest file which identifies the directory as a Swift package. It describes the characteristics of the package, for example, the name, its own dependencies, supported Swift versions and more. You typically do this by constructing an instance of Package, a type included in the Swift PackageDescription framework, with all needed parameters.

The Sources directory

The Sources directory contains all the sources of your package. They are divided into folders which represent the different buildable components of the package, called the targets.

The Test directory

The Tests directory contains all the unit test for the different targets.

How to create a new Package

You can create a new Swift package by going to File -> New -> Swift Package... or using the ctrl-Shift-cmd-N shortcut. This will open a dialogue where you need to specify the name of the package. By default, Xcode will create a new project for your package. You can also add the package straight into your project by selecting your project in the Add to pull-down button. To link the code with your app’s target you add the product to the Frameworks, Libraries and Embedded Content of your target. The last option is great for when you want to modularize parts of your codebase.

How to add a package as a dependency to your project?

To add a package as a new dependency to your project you use the Add Package Dependency... option that can be found under File -> Swift Packages. This option will open a dialogue that guides you to all the steps of adding a package:

  1. Pick the project you want to add the dependency to.
  2. Choose the package repository. If you are logged in with, for example, a GitHub account it will show all your repositories. You can also directly add the URL of the repo in the text field located a the top of the dialogue.
  3. Choose package options. In this step, you specify version rules etc.
  4. Add the package products to your apps targets to make sure they get linked when you compile your project.

And you’re done!

Publishing your package

Publishing your package is as simple as creating a git repository for your package and pushing it to a service like GitHub. Once you do this it is important that you correctly make use of semantic versioning, as other developers projects will now depend on your package.

What about resources

A current limitation of the Swift Package Manager is that it does not yet support resources. This means that it, for example, is not possible to create a UI framework that makes use of icons that are contained in an asset catalog or a data layer framework that contains a Core Data Model file. Have a look at the following proposal and thread to get to know more about the plans of supporting this in the future.

Conclusion

With Xcode 11 the process of creating, adding, updating dependencies, etc. can be done straight from Xcode which makes it very straightforward and a pleasure to use. Are you ready to start using the Swift Package Manager for your project?

Contact me on Twitter @kairadiagne if you have any questions, comments or feedback.