Introduction
Back in Swift 3.0 Apple introduced Swift Package Manager (SPM), which is an easy way to manage dependencies and distributing Swift code that is re-usable.
Creating and consuming packages feels very "Swifty" (unlike dependency managers like CocoaPods) and all tooling is nicely integrated in Xcode.
However, when working on a daily basis with SPM you might run into some problems that slow down development. This topic is aimed to cover those topics and share tips/best practices to make working with SPM as smooth as possible.
Managing dependencies
In Xcode managing dependencies is super easy.
- In the file navigator click on your project
- The project view will open. Make sure to select your project (at the top) and not one of your targets!
- Click on the "Package Dependencies" tab
- Click on the "+" button to add a dependency
- Search for the package you'd like to add, or enter the Package URL, which is usually the URL to the GitHub project
ℹ When actively developing a SPM package, you might consider adding a "Local" package. See for more details the section below
Working with frequently mutating packages
When working on a Swift Package, you might find yourself making changes quite often. There are three options to speed up the development process.
- Specify a
branch
- Specify a
Commit hash
- Add the package locally
Specify a branch
- In Xcode open the "Package Dependencies" tab (found in your project settings)
- Double click on the dependency you're working on
- A window will open where you can specify so called "Rules", by default the "Version" rule will be selected
- Click on the "Branch" rule and enter the name of the branch you're working on
- Click "Done"
Specify a Commit hash
- In Xcode open the "Package Dependencies" tab (found in your project settings)
- Double click on the dependency you're working on
- A window will open where you can specify so called "Rules", by default the "Version" rule will be selected
- Click on the "Commit" rule and enter the commit hash you want to use
- Click "Done"
Once you've pushed the changes to your SPM package, you need to let Xcode know there is a new version available. To do so, click on "File" -> "Packages" -> "Update to latest package versions".
Add the package locally
Adding the package locally is the fastest way to iterate during development, since it doesn't require you to push to Git/wait for the build to succeed/update the package/etc..
WARNING: If you already have the SPM as a dependency and you want to switch to "Local development", please make sure you remove the dependency first by selecting it and hitting the "Backspace" key.
- In Xcode open the "Package Dependencies" tab (found in your project settings)
- Click on the "+" icon
- Click on "Add Local..."
- Browser to the folder on your Mac where the package is stored
- Click on "Add Package"