DEV Community

Cover image for How to create an example app for your SPM package
Kaww
Kaww

Posted on • Updated on • Originally published at kaww.hashnode.dev

How to create an example app for your SPM package

When working on a Swift package, it may be quite difficult to test that the code we are writing works properly.

Beyond writing unit tests, an easy way to test a package would be to configure a demo project right inside the repository. This has non-negligible advantages:

  • It allows us to write at the same place the package code and the demo code without any friction.
  • It allows us to configure several targets, for instance, an iOS and macOS demo apps.
  • It serves as documentation code with usage examples for other developers that will be able to browse the code on Github.

In this article, we will see how to add and configure a demo app project for an SPM package.

Create the demo project

First, we need a Swift package. If you don’t have any and want to learn how to create one, check out my article “How to create an SPM package with SwiftUI”.

Now we will create the demo app project. In Xcode, go to File > New > Project...then choose for instance an iOS app. By convention, I will name it as my package name suffixed by “demo”. Here, “MyPackageDemo”. Some also suffix it "Example".

Package creation

Then save the project at the root location of the Swift package:

Package save location

Close Xcode windows, then go to the new Xcode project directory and open the .xcodeproj file.

Configure the demo project with the package

The trick here is to import the SPM package by referencing its location on the disk, instead of importing it from GitHub.

To do so, go to the main project file then go to Project > Your project > Package Dependencies.

Package Dependencies

Press the “+” button and select “Add local”. The Finder opens, go to the root location of the SPM package, where the Package.swift file is (and that now also contains the demo project folder).

Add package locally

The SPM package is now imported, we can see it in the file explorer section.

The final step will be to link the package’s library to our demo project target. To do so, go to the main project file then go to Targets > Your project target > General. Scroll down to find the section Frameworks, Libraries, and Embedded Content then click the “+” button.

Frameworks, Libraries, and Embedded Content

A window opens, find the SPM package and select the library icon.

Library selection

All good! The project is fully configured, it's now possible to compile both the package code and the demo app, simply by changing the build target on Xcode’s top bar.

We can now use our package's code in our demo app's code and push all that on the package’s GitHub repository.

Note that if you want to work in that configuration, you need to open the .xcodeproj file with Xcode. And if you just want to open the SPM files you can still open Xcode at the package's root location, but you won’t be able to run the demo app this way.

Wrap up

In this article, we learned how to create and configure a demo app project for an SPM package to simplify its development.

I hope this article has been helpful to you. If you have any questions or feedback about this article, don’t hesitate to contact me on **Twitter!

Top comments (0)