View our current server-side Swift resources and read the deprecation announcement for Swift Sandbox and Package Catalog.

In this short tutorial we will install and configure Swift on the Mac or Linux, then walk through the creation of a simple project, and finally demonstrate our favorite new features in Swift 3.0: automatically including dependencies into projects.

The Swift Package Manager is integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

The IBM Swift Package Manager enables developers to easily search and discover Swift Package Manager compliant packages and libraries for both client and server side development.

Install Swift

Install Swift for MacOS

Swift 3.0 is available as part of Xcode 8.

Install Swift for Linux

Currently, Swift is only available on Ubuntu 14.04 and 15.10. For this tutorial, we will be going through the steps on an Ubuntu 14.04 system.

Before we get started we need to make sure that the system has the necessary prerequisites:

$ sudo apt-get install clang

Next we need to download the current version of the Swift 3.0 release for Ubuntu 14.04 from https://swift.org/download/

download

Once the file has been downloaded we need to extract it running the following command:

$ tar xvzf swift-3.0-RELEASE-ubuntu14.04.tar.gz

This will create a directory called swift-3.0-RELEASE-ubuntu14.04 which we can then move to a more permanent location.

To make it easier for us to access the swift executable we can add the path to the bin directory by running the following command:

$ export PATH=/path/to/swift-..-ubuntu14.04/usr/bin/:"${PATH}"

We can test the installation by running the following command:

$ swift --version
Swift version 3.0 (swift-3.0-RELEASE)
Target: x86_64-unknown-linux-gnu

Create and Build a new Swift Project

Now that we have an environment ready to develop Swift on our MacOS or Ubuntu system, we can create a simple executable project. First of all, we need to create a project directory and move into it.

$ mkdir spm_test
$ cd spm_test

Once we are in the spm_test directory, we can initialize the executable by running the following command:

$ swift package init --type executable
Creating executable package: spm_test
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/main.swift
Creating Tests/

The swift package init command has created an executable package directory structure for us. Running the swift build compiles the source code into an executable that can be run.

$ swift build
Compile Swift Module 'spm_test' (1 sources)
Linking ./.build/debug/spm_test
$ .build/debug/spm_test
Hello, world!

We’ve been able to install the Swift runtime on our system, create and build a new project, and run the executable without editing any source code, and with only a handful of steps. We still haven’t done anything interesting, so let’s include an external library into our code that will allow us to parse JSON easily.

Find an External Package to include into your Project

Since we know that our project will need to process some JSON, and we heard that about libraries that make it even easier to do in Swift, we can go to the Swift Package Catalog and see if any packages would be useful to us.

spc

We can use the built-in search to help us find something useful.

spc-search

After looking around we found one package we really liked SwiftyJSON and, we hope that it could make working with JSON easier.

spc-swiftyjson

The package page has a couple of things that make development on Swift 3.0 easier. First of all, we can see in the right column that this version of SwiftyJSON is compatible with Swift 3.0

spc-project-version

In addition we can quickly see if any additional dependencies will be included automatically by SPM, and we can see what other packages are dependent on the package.

spc-project-deps

We can also see what the current version is and when it was last updated.

spc-project-published

It is also possible to see how many people have favorited the project in the catalog or have starred the project on GitHub.

spc-project-favs

Since the developer for this project has added some sample code, you can press the “try in Sandbox” button to run a sample in the IBM Swift Sandbox.

spc-project-try-in-sandbox

This will bring up a window that will allow you to select what sample to try in the IBM Swift Sandbox.

spc-package-try-in-sandbox

Include External Package into your Project

The project now includes two source files which have been pre-populated; Package.swift is a file that contains information about the project, such as the project name and any dependencies that it may have, and Sources/main.swift which is the project’s main source file.

The Package.swift file should look like this:

import PackageDescription

let package = Package(
name: "spm_test"
)

This might be a good time to add the placeholder for dependencies. Simply add the highlighted code and save:

import PackageDescription

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

In order to include the package into our code, we will need to register SwiftyJSON as a dependency. This involves adding a line into the dependencies: [] section of the Package.swift file. The Swift Package Catalog makes this easy by providing you with the exact line that you will include. Simply press the “Include project in your code” and the line that you need to include is displayed.

spc-project-include

If you press the “Copy” button, the line will be placed into your clipboard.

spc-project-copied

At this point we can edit the Package.swift file again and paste the copied text inside of the dependencies brackets.

let package = Package(
name: "spm_test",
dependencies: [
    .Package(url: "https://github.com/IBM-Swift/SwiftyJSON", majorVersion: 14, minor: 2)
]
)

The SwfityJSON package is now available for us to use in our project. Edit the Sources/main.swift file to try out SwiftyJSON. We simply need to import the package in order to start using the functionality.

import SwiftyJSON

let json = JSON(["name":"manuel", "hobby":"photography"])

let name = json["name"].stringValue
let hobby = json["hobby"].stringValue

print("My name is " + name + " and my hobby is " + hobby)

At this point we can build the project again. The Swift Package Manager will now connect to Github and clone the package that has been listed as a dependency, compile it, make it available to the project, and create the executable.

$ swift build
Cloning https://github.com/IBM-Swift/SwiftyJSON
HEAD is now at 73b523a 3.0
Resolved version: 14.2.0
Compile Swift Module 'SwiftyJSON' (2 sources)
Compile Swift Module 'spm_test' (1 sources)
Linking ./.build/debug/spm_test

When we run the resulting executable again, we now see that external functionality from a Github hosted package has been added, simply by adding a single line to the Package.swift file.

$ .build/debug/spm_test
My name is manuel and my hobby is photography

Conclusion

In this tutorial we walked through the installation of Swift on Mac or Ubuntu 14.04, the creation of a simple executable, and the inclusion of external packages into a simple executable. In addition, we discovered how the Swift Package Catalog can simplify the process of finding the right package, and easily to include the package in a project.

To learn more about our Swift@IBM program, visit our development center at developer.ibm.com/swift/

2 comments on"Learn how the IBM Swift Package Catalog makes working with the Swift Package Manager even easier"

  1. Author make my not a day hi make my month because i search a long time tutorial some thing like. And real stuff will if some one will meet qt with swift this will increadible!!!!

Join The Discussion

Your email address will not be published. Required fields are marked *