Using Swift Package Manager libraries in Playgrounds

Update #2: SPM-Playground is now Arena

Update #1: Version 0.5.0 has been released

One of the defining features of Swift has always been Playgrounds, the REPL on steroids that allows fast prototyping and iterating over algorithms and code. And while it works great for Apple’s built in libraries and SDKs, it’s never been quite as smooth an experience with third-party libraries.

The recent addition for Swift Package Manager support in Xcode 11 has gotten us a step closer but it’s still not 100% straight-forward to get set up. It takes quite a few steps to get there.

So when I wanted to give John Sundell’s new Plot project a spin, I not only had to jump through the usual hoops to get it up and running in a Playground. I also ended up automating the process first in a shell script and then in a more polished command line tool, which I am now releasing as SPM-Playground.

The tool is very simple to run:

~  spm-playground -d https://github.com/johnsundell/plot
🔧  resolving package dependencies
📔  libraries found: Plot
✅  created project in folder 'SPM-Playground'

After creating the project, it opens the folder in the Finder. When you open the Xcode workspace and select the Playground file you’ll see the SPM project’s library has already been set as an import. Simply hit ⌘-B to build the dependency and starting playgrounding!

Note that you may need to make sure the correct platform is selected.

Swift Playground using an SPM dependency

And with this written up, I can get back to what I actually wanted to do – check out John’s Plot library.

Did you find this post useful? Do you have any questions or comments? Please get in touch and let me know via Twitter or email.