Create a Swift Package and use it in a Local Xcode Project
Create and Import a Swift Package locally
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
- Create a Swift Package
- ‘Git’ up the package
- 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.
cd Desktop/MyLibrary
git init
git add .
git commit -m "Initial Commit"
git tag 1.0.0
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.
file:///Users/Johann/Desktop/MyLibrary
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!