How to make and use a Swift Package

Scott
3 min readApr 3, 2021

Your file structure may look similar to this:

Your modular code goes into Sources. Your tests go into Tests.

Package.swift outlines the dependency graph.

You can modify and customize the package as so:

More info on customization.

You can’t have multiple languages per target, but each target can have a different language.

You can create dependencies like so:

Alternative methods here.

If you’d like to conceal your implementation. You can distribute your package as a binary.

For .string .storyboard asset catalogues etc.

Make sure you want anything that is to be shared is marked with public in your Swift Package.

Add your package to git tracking.

you can clone your remote repo and drag in your swift package (just drag in the whole thing), or you can follow these steps to create your repo from the command line.

Add a tag on Github.

Make sure your Target is correct! if it is targeting the wrong branch you will get some pretty obscure and difficult bugs when you try to update!

You should double check your tag by clicking the associated commit and make sure it looks correct. `a66e51d1` is that right?

Time to access your package in a project.

  1. Copy the .git url for your project.
  2. Create a local project.
  3. File->Swift Packages->Add Package Dependency

4. You should be able to paste the .git link in the box provided.

5. Now you should see it in your file navigator.

5. And you should be able to use it in your code with an import statement. For example: import MyPackage1

6. Only Types that are marked as public will be accessible.

If you want to add a tag via Terminal then you can follow these steps. Then add a tag using semantic versioning.

$ git tag -a v1.4 -m "my version 1.4"
$ git tag
v0.1
v1.3
v1.4

Tags are attached to a commit, you can see that here:

sl@Scoots-iMac PackageTest2 % git show commit ee36fb72e05f0cd9da768471703710b3168864a0 (HEAD -> develop_first, tag: v1.0.0, upstream/develop)
Merge: 8755501 99d4659
Author: Scott <scottlydon18@gmail.com>
Date: Sat Apr 3 13:56:41 2021 -0700
Merge pull request #1 from scott-lydon/develop_firstfirst

By default, the git push command doesn’t transfer tags to remote servers. You will have to explicitly push tags to a shared server after you have created them. This process is just like sharing remote branches — you can run git push origin <tagname>.

$ git push origin v1.5
Counting objects: 14, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (14/14), 2.05 KiB | 0 bytes/s, done.
Total 14 (delta 3), reused 0 (delta 0)
To git@github.com:schacon/simplegit.git
* [new tag] v1.5 -> v1.5

--

--