How to create an example app for your SPM package
Tags: swift, swiftui, xcode, github
Published: 2023-03-08 12:57:55

---
title: How to create an example app for your SPM package
published: true
description: A beginner's guide to Swift Package Manager
tags: swift, swiftui, xcode, GitHub
canonical_url: https://kaww.hashnode.dev/how-to-create-an-example-app-for-your-spm-package
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4dlfjqxfjha4e44y8z6h.png
# Use a ratio of 100:42 for best results.
# published_at: 2023-03-05 17:58 +0000
---

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](https://kaww.hashnode.dev/how-to-create-an-spm-package)**”.

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ngtykktq98zr6a4d752i.png)

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

![Package save location](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fkl2kwfwxkgei78rx1zl.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x8v25p5z40ejjyiwv8zg.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxcfzay769n5qiuo4wou.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pgjfvdqbc6be41cjod6m.png)

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

![Library selection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yemd6heqp0au95rptcg5.png)

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](https://twitter.com/kaww)**!*