In the last post, we talked about some theory around Swift Package Manager. Now let’s talk how to actually use it with Xcode. How to add, edit our packages and also how to update its version in project.
We will enrich our app with some UI. In order to do that we will create the Design Theme package.
Create a package
Firstly let’s start with creating the Design Theme package.
We can create a package in two ways: via Xcode or Terminal.
To create package via Terminal first you have to create a folder of the package and then inside this folder initialize package.
To do the same in Xcode you have to select File -> New -> Swift Package (or Control+Cmd+Shit+N) from tab menu.
Then specify the name and folder of the package. You can also choose if the package should be included in existing project and establish git repo. After you click Create you will create a library package which will automatically open in Xcode window.
One way or another we will end with a bare-bone package. From here we can start adding some code to it and eventually push it to the repository.
Adding a package
We created the Design Theme, its time to add it to the project. To do this open Swift Packages tab in Project settings:
Then click plus sign and when menu appears select package you want to add (Design Theme in our case). If we have linked Github account to Xcode it will automatically show us our packages, if not or using 3-rd party package we need to provide URL for package.
Next step is setting the package options: version, branch and commit. By default selected version will be last updated from repo on master branch
The last step after the package is downloaded is to decide whether add or not package product to App target, usually, we will add it
Now the package is added to our App (we can see it in Swift Packaged Dependencies in Project Navigator) and we can start importing it!
We can also add the package into other packages, by adding it into the dependency section of our Package.swift file
Then we need to resolve packages by File -> Swift Packages -> Resolve Package Versions
The package will be fetched, added to dependency section (along with its dependencies) and we can start using it.
Using the method above we can also add our local repository. Simply when passing URL for package prefix it with file:// and after it type path to folder. Remember to initialize Git Repository firstly!
Editing a package
So we added a new package to the project, but now we want to add some new features into it, how to that? As previously there are two ways of doing that.
Firstly we can add new code in a package, commit and push it, then update package in the project.
But what if we would like to check how new code behaves in an app before pushing it? Editing package inside our app is very simple. Firstly checkout our local code of package to have the latest version. Then simply drag and drop a folder into Project Navigator. That way we will have access to the local package and we can start editing it!
If we would like to edit package inside another package (for example enrich the Cards project with some fancy UI) we can do it with three methods. First two are the same as for an app. The third is changing URL in the dependency section of Package.swift to our local path. But that way we will not have access to the code inside the main package.
The documentation says that there is also a way to edit package using Terminal:
swift package edit Foo --branch bugFix
swift package unedit Foo
But this option didn’t seem to be working on Xcode 11.0, probably will be fixed later on.
We have create, added and edited a package. Now we have commit and push changes to the repository with a new tag. Then we can do the last thing – update the app with a new version of the package. What only we have to do is to select File -> Swift Packages -> Update to Latests Package Version and voila, our package is now at new tag.
In this post, we touched the core of the Swift Package Manager and looked at how it can be handled by Xcode. We gained knowledge on how to create, add, edit and update a package. Now we can develop our modularized app with ease. The last thing of SPM world is Package.swift file and that’s the thing we will talk about in next post. See you there!
We process cookies and make them available to Google Analytics (a service provided by Google, Inc.) to improve the performance of the website, to learn your preferences about using it and to tailor it to your needs. The data will be anonymised before being transmitted. If you do not agree to this, you may disable cookies in your browser. If you do not change your browser settings, you accept the fact that it saves cookies.