Swift Package Manager is a fantastic tool for managing dependencies, and over the years, Xcode support for SwiftPM has improved, streamlining the development workflow.
In Xcode 13, discovering and managing third-party Swift packages became a little easier with the introduction of Swift Package Collections (add link).
A Package Collection is a JSON file that describes a curated list of packages. The collection can be shared directly or online. It can also be signed to prevent tampering. This can be a great way to share dependencies across an organization or make frequently used packages more accessible.
Xcode is pre-configured with a single Package Collection published by Apple. Adding an import statement for one of the packages in this collection results in a fix-it prompt to add the dependency to the project. Adding new collections can enable this same fix-it for other Swift Packages.
The Swift Package Index maintained by Dave Verwer and Sven A. Schmidt makes it easy to find Swift Packages as well as Package Collections.
Add Package Collection in Xcode
A great third-party example is the open-source libraries authored by the folks at Pointfree. Pointfree publishes a number of fantastic utilities for writing code that is both testable and ergonomic.
Steps to add a collection:
- Within Xcode, click File > Add Packages…
- Under Package Dependencies, click the +
- In the bottom left corner of the package browser, click the +_ and select Add Package Collection…
- Paste the URL to the Package Collection
- Confirm by clicking Add Collection
Once added, it is easy for Xcode to find and import your favorite packages.
If you try to import a module that isn’t already a dependency of a project, Xcode will give you a warning. But if the package is part of a package collection that is registered with Xcode, then Xcode will provide a fix-it that will automatically add the dependency to the project.
Add package collection from the command line
Package Collections can also be added to SwiftPM and made available in Xcode from the command line.
$ swift package-collection add https://swiftpackageindex.com/pointfreeco/collection.json
Package Collections aren’t going to save you hours of development time, but they will make your favorite packages more accessible while staying in the editor flow.