Playing with the Swift Package Manager - A Tutorial

Roy Marmelstein
Swift and iOS Writing
2 min readDec 7, 2015

--

Swiftmas came early last week with Apple’s festive open-sourcing of Swift. There were plenty of exciting new things to discover but one that stood out for me was the Swift Package Manager.

Most modern languages come with an official solution for code distribution and it was nice to see Apple working on its own alternative to Cocoapods and Carthage.

As soon as I heard about the SPM, I wanted to support it in my open source projects (Localize, Format and PhoneNumberKit). Having spent some quality time with the docs - the good news is that using it is way easier than I originally anticipated (🤗). The bad news is that it currently doesn’t support iOS, watchOS or tvOS (😭).

At this time the Package Manager has no support for iOS, watchOS, or tvOS platforms.

It’s clearly early days for the Swift Package Manager and support for all of the above platforms is on its way (EDIT: turns out you can support OS X and Linux system dependencies). Despite these temporary limitations, here’s a quick tutorial for those who wish to try it.

First Step

Install the Swift 2.2 snapshot from https://swift.org/download/

After installing, run this command in the terminal to make the new Swift tools available:

export PATH=/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin:"${PATH}"

Creating a module

Create a Package.Swift file at the root folder of your module. It should contain the following code (change the package name):

import PackageDescriptionlet package = Package(
name: "Format"
)

You will need to make sure your .Swift files are in a Sources folder.

Using a module

Once again, you will need to create a Package.Swift file at the root folder. Inside it, put the following code (change the url to your git repo):

import PackageDescription

let package = Package(
name: "Format",
dependencies: [
.Package(url: "https://github.com/marmelroy/Format.git", majorVersion: 1),
]
)

In the terminal, run:

swift build

Voila!

--

--