Mobile
Article

Dependency Management with the Swift Package Manager

By Chris Ward

Swift’s journey into a fully fledged cross-platform language continues as it’s contributors focus on the version 3 release. Any language that wants a long-term existence needs a way of adding functionality that doesn’t require the core developers to add every request. This is typically in the form of package or dependency management, and the Swift Package Manager (SPM) will be one of the many features added to Swift 3.

But something not being officially released has never stopped inquisitive developers experimenting in the past. In this article I will introduce the SPM, show you how to install it and existing packages, and how to create your own.

Note: You should be able to follow the steps I will present on an OS X or Linux platform.

Living on the Edge with the Swift Package Manager

The SPM is not part of the current official Swift release and you will need to install trunk development snapshots of V3 to use it. This is not too difficult and you can find instructions here.

As installing this snapshot could break your setup for production app development, I recommend you install swiftenv, which lets you switch between installed versions of Swift and is super useful for experimenting with Swift versions. Once you have installed swiftenv and you have a trunk development release active, check you have the package manager by running:

swift build --version

You will hopefully see something like Apple Swift Package Manager 0.1.

If you want to use XCode instead then it will manage different Swift versions for you. Open XCode -> Preferences and set the Toolchains under the Components section.

Change Toolchains

Note: Swift is undergoing rapid development and things change all the time, breaking projects. For this tutorial I used the February 8th snapshot for greater compatibility. Now you can see why I mentioned how to switch versions and you will do it a lot until version 3.

Using Existing Packages

Many existing Swift packages are available, but currently no central listings service like NPM exists, so finding them can be hard. One option is The IBM Swift Package Catalog, but it contains a mixture of CocoaPods, Carthage and Swift packages. I expect there will be an ‘official’ list sometime in the future.

To add an existing package as a dependency to a project, create a file named Package.swift and add the following:

import PackageDescription

let package = Package(
    name: "SitePointSPM",
    dependencies: []
)

This is the basic structure of a package file where you set a name, which is a package itself and an empty array. To add a dependency, change dependencies[] to:

...
dependencies: [
  .Package(url: "https://github.com/kylef/Curassow.git", majorVersion: 0, minor: 4),
]
...

This downloads a dependency from a url (generally github) with a specified version using semantic versioning.

Create a Sources folder and in it, create Main.swift. Add the following code:

import Curassow
import Inquiline


serve { request in
  return Response(.Ok, contentType: "text/plain", body: "Hello World")
}

This code uses the Curassow and Inquiline packages to configure and start a basic http server.

Execute swift build --configuration release to run this simple app. Notice that when you build for the first time the Swift build process will download the dependencies declared in your package file, plus the dependencies that it declared.

Creating Your Own Package

You construct a Swift package in the same way as a ‘normal’ application. But a package generally consists of and includes source files located in a Sources directory. The sample application provided by Apple is a great example to learn the potential.

In this example, the PlayingCard package defines a PlayingCard. Then the DeckofPlayingCards package imports the PlayingCard package and uses it’s methods and objects to create a randomly shuffled Deck of PlayingCards.

Here Be Helpful Dragons

Following this introduction you likely hit problems installing and using the Swift package manager. This shows it’s certainly not ready for production applications. But, it’s simple to use and create packages for and whether you decide to wait for a stable Swift 3, or jump right in and update your code every time something breaks, the Swift package manager is another puzzle piece in making Swift a true full stack language in the next 12 months.

What are your thoughts?

No Reader comments

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in Mobile, once a week, for free.