Create a Swift Package and use it in a Local Xcode Project

Create and Import a Swift Package locally

Johann Fong
Nerd For Tech
3 min readFeb 26, 2021

--

At some point in time during your swift journey you might you would want to create your own Swift Package. If you are like me and prefer to have things working on a local repo before pushing it to a remote repository this is a guide for you.

I’m using Xcode 12.4 but the concept should work for you if you are using 11 and later.

The Recipe

  1. Create a Swift Package
  2. ‘Git’ up the package
  3. Import your package and build!

1. Create a Swift Package

To create a swift package, launch Xcode. Go to File > New > Swift Package… Alternatively, you can use the keyboard shortcut ⌘⌃⇧N.

Navigate it to your ‘Desktop’, name your package and click ‘Create’. In this case, I have named my package as the default name ‘MyLibrary’.

2. ‘Git’ up the package

Swift Package Manager requires Git at runtime as well as build-time.

So what we have to do now is to Git initialize our package and tag it with a version so we can import it into our project.

Remember to change ‘MyLibrary’ to what you have named your package to be.

We have now ‘Git’ up the package!

3. Import the package and build!

We have come to the final step! we can now import and use the Package.

To do so open the desired Xcode project you want to use the package in.

In the Project Navigator select the blue icon with your project name.

In the main view select the white icon with your project name under ‘TARGETS’ and scroll down until you see ‘Frameworks, Libraries, and Embedded Content’.

Click on the ‘+’.

Next Click ‘Add Other…’> Select ‘Add Package Dependency…’.

In the search bar enter the location of your package. In my case, ‘MyLibrary’ is still on my desktop so I’ll use the following.

Click ‘Next’ > Click ‘Next’ Again> Click ‘Finish’

Press ⌘+B to build your project. After which you can import your package

That’s It!

In the next article, I’ll share how to create a Swift Package with a Demo Project in a single workspace! Look out for it!

Meanwhile, please feel free to comment below, if you have any questions.

Thanks for reading!

--

--

Nerd For Tech
Nerd For Tech

Published in Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Johann Fong
Johann Fong

Written by Johann Fong

Documenting my Development in Software Development