Use Swift Package Manager to Add Dependencies
SPM + Xcode + THAT GREAT PACKAGE
If you have reusable modules you might already use Swift Package Manager (SPM) to manage these dependencies for you. In this article, we will use SPM to fetch and use third-party code in our project.
Difficulty: **Beginner** | Easy | Normal | Challenging
This article has been developed using Xcode 11.4.1, and Swift 5.2.2
What are dependencies anyway?
Something you depend on. Now, come on, even I can do better than that.
Imagine you want to use an external library for something a little tricky, like networking.
We can call that library Alamofire, and indeed many programmers use this for their projects as it avoids using their own network library and can feel secure that the library is already in use and well-tested in a variety of contexts.
Now there is an in-built problem here. If the third-party dependency has a bug in it and they fix it; do you want to use that new version with the fix (probably) and do you want to use that new version with a host of new features (umm, probably — if it doesn’t break the code that I’m working on). Which means that dragging files into your project is a quite immature and unhelpful way of managing depenencies in a larger project. Therefore *package managers* have been developed to help us out.
Swift Package Manager isn’t the only option for managing dependencies and giving us our desired third-party libarary (and version of the said libarary).
Find those dependencies
With so many third-party libaries avaliable for Swift, how do you pick the right one for you? You’ll need to consider (amongst other questions) the following:
- support (updates etc.)
- compatibility with your current Swift version
- platform support of the libarary
To help you pick woudn’t you like a nice interface giving you the avaliable options?
There are a couple of different sites you can use — swiftpack.co and swiftpackageindex.com are two that spring to mind — but for the purposes of this tutorial I’m going to use the latter of the two for this article.
This example is going to simply be to use Alamofire as a dependency in a new Single View Application. So after you’ve set up the new application (I’ve called mine
AFNetworkingPM) follow the steps below:
Navigating to packages
You can navigate to packages by clicking the project name and the
Swift Packages tab.
Adding the package
We can click the
+ icon in order to add a Swift Package:
Which brings us to the following screen:
Xcode will then verify the link, and present some wonderful options for us.
Where the default is `Version — Up to Next Major` which since updates up to the next major version should *not* introduce breaking changes to your code, but also keeps your project up to date (apart from that). If you want an explaination of this versioning — read about semantic versioning
Alamofire will then be resolved and fetched. You can then add the package to your target:
The glib answer
You can now import Alamofire at the beginning of any file using
import Alamofire — that’s it!
Alamofire is a networking library, so can we do something useful with it? Now I’m not going to use MVVM-C or in fact any architecture as this is an article about Swift Package Manager, but I will get it to download from a public API. — in this case I’ll use HTTPGet to do just that. Since the request is made to a https site we don’t need to worry about App transport security
The idea of using Package Manager is a simple one — get those dependencies added right into Xcode. This article has hopefully helped you to do just that, and to get on with the more interesting problems that can be solved using your valuable time.
I hope this article has helped you out!
If you’ve any questions, comments or suggestions please hit me up on Twitter