Editing A Swift Package

When I want to make changes to a Swift package, I find it useful to do it in the context of a project that depends on that package. Unfortunately, you can’t directly edit a Swift package you add as a dependency to an Xcode project. It’s read-only and managed by Xcode. What you can do is add a local copy of the package that overrides the package dependency.

Last updated: Nov 10, 2022

Swift Package Dependencies

I previously covered creating Swift packages in Xcode 11 and then adding them as a dependency to a project. Xcode downloads the package files from the remote repository:

Xcode file navigator showing Swift package dependency

You can browse the source files in the package, but you cannot edit them in Xcode (which is probably for the best).

Editing A Standalone Package

You always have the option to clone the package repository to a new folder:

$ git clone <URL of Git repository>

Finder showing Package.swift file

You can then open the Package.swift file in Xcode and work on the package directly:

Package in file navigator

After you’ve made your changes, commit the package back to the remote repository.

This is cumbersome if you want to try your updated package with an existing Xcode project. To get Xcode to download the updates from the remote repository you either need to tag the commit with a new version or change the project dependencies to refer to the specific commit:

Package dependency using commit identifier

Luckily, there’s a better way.

Editing A Local Copy

  1. Clone the package repository to a local directory as before.

  2. Drag the whole folder into the Xcode project that has a dependency on the package.

  3. Xcode overrides the package dependency with the same name as the local package. Notice in the screenshot how the Swift Package dependency disappears from the file navigator.

    File navigator with local package override

  4. Make your changes to the local package and test them with the project before pushing the changes to the remote repository.

  5. Release the changes by tagging the commit and then pushing the tag:

    Push tag

To switch back to using the remote package:

  1. Delete the local copy of the package from the project.

  2. Make Xcode update the package dependencies File > Swift Packages > Update To Latest Package Versions.

  3. You should now see the latest version of the remote package in the file navigator:

    Updated package dependency