Search code examples
xcodeswift-package-managerxcode11.4

Editable Swift package in Xcode project?


I'm working on a few different apps in Xcode, and I use a Swift package for my "common" library. In each project, I add the common Swift package by going to the project's "Swift Packages" (next to "Info" and "Build Settings") and entering a local "file:///..." url. That checks out a read-only version. Everything works pretty well, but if I'm doing a lot of editing of the common files, it gets annoying. I have to:

  • Switch to another Xcode window, which has the Swift package open in read-write mode.
  • Edit the files, commit them to git, tag the commit
  • Back in the app project, menubar > File > Swift Packages > Reset Package Caches (assuming I moved the tag, rather than creating a new one)

Is there a way to streamline this, so I can edit and compile files in the Swift package without having to do the git steps every time?


Solution

  • I've discovered that I can do this, so I'm answering my own question.

    You can use a "local package" as described in this short article from Apple: Developing a Swift Package in Tandem with an App

    I keep the same configuration as before — my app project still has a Swift package dependency defined, and that dependency still points to the same URL for the git repo. But now I also checkout/clone a local copy of that repo, into my app's project directory, or maybe one level up if I'm using an Xcode workspace. Then I drag that cloned directory to the Xcode project navigator. As soon as you drop that folder there, Xcode makes some adjustments to its UI, to reflect that it's using it as a local version of the package. You can edit the files there, and push changes back to the main repo. If you delete that local clone, Xcode goes back to using the read-only version like before.