Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a demo app target to a Swift Package?

I'm looking to include a demo app with a Swift Package of custom controls that shows implementation demos for each of the controls. Is there a way that I can include this in the Swift Package (and also develop/test the controls directly inside the package this way)?

like image 644
joshd Avatar asked Sep 01 '25 02:09

joshd


2 Answers

Edit: 2024-11-09

  • Step 2 updated: Xcode no longer allows packages to be an ancestor of a project
  • Removed steps 6 & 7 (adding Frameworks)

Here are the steps that worked for me on Xcode 13/14/15:

  1. Create a package called MyLibrary

enter image description here

  1. Create a new Project App MyLibraryDemo somehwere, but NOT inside the package directory*

enter image description here

  1. Open the project's .xcodeproj file

  2. Go to PROJECT > MyLibraryDemo > Package Dependencies and press the 'plus' symbol

enter image description here

  1. Press Add Local and select the directory containing your pacakge

enter image description here

  1. Now you can import your MyLibrary package into your example app, and edit / update your package from within you demo project.

enter image description here


Some Notes:

  1. If your package / demo has other dependencies, and those other dependencies get updated, you may have to clear DerivedData before doing an update

  2. You may also have to close & re-open Xcode, especially if you've updated dependencies, but it still says "Cannot find package MyLibrary"

like image 140
tospig Avatar answered Sep 02 '25 15:09

tospig


What kind of app? SwiftPM only directly supports macOS/Linux executables.

If you want an iOS/tvOS/watchOS one you will need an Xcode project that links using a relative path to find your package. This is done by creating the example app and dragging the Package's containing directory into the Xcode project. You can then link the library. If you put this example app in the repo with the package then it will be distributed along with anyone who clones the package.

One important thing I have noticed (which is almost certainly a bug in Xcode) when doing this myself is that the Package.swift and Example.xcodeproj cannot be in the same directory. You have to nest the Xcode project into another directory or it will have issues building/linking a lot of the time. So if you run into any issues With what I have suggested also try this bandaid.

I have created an example on GitHub here that works for Xcode 11.3.

like image 45
bscothern Avatar answered Sep 02 '25 17:09

bscothern