Book Image

Mastering Swift 3 - Linux

By : Jon Hoffman
Book Image

Mastering Swift 3 - Linux

By: Jon Hoffman

Overview of this book

Swift is a modern, fast, and safe programming language created by Apple. Writing Swift is interactive and fun, the syntax is concise yet expressive, and the code runs lightning-fast. Swift’s move to open source has been embraced with open arms and has seen increased adoption in the Linux platform. Our book will introduce you to the Swift language, further delving into all the key concepts you need to create applications for desktop, server, and embedded Linux platforms. We will teach you the best practices to design an application with Swift 3 via design patterns and Protocol-Oriented Programming. Further on, you will learn how to catch and respond to errors within your application. When you have gained a strong knowledge of using Swift in Linux, we’ll show you how to build IoT and robotic projects using Swift on single board computers. By the end of the book, you will have a solid understanding of the Swift Language with Linux and will be able to create your own applications with ease.
Table of Contents (24 chapters)
Mastering Swift 3 - Linux
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
2
Learning About Variables, Constants, Strings, and Operators

Using the Swift Package Manger


According to Apple, the Swift Package Manager is defined as:

A tool for managing the distribution of Swift code. It's integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

The first part of the previous statement means that the Swift Package Manager can be used to manage the distribution of modules. Some applications may have all their code organized into a single module. More complex applications may separate their code into different modules. As an example, if you were developing an application that communicates with another device over Bluetooth, you may want to put the Bluetooth code into a separate module, so you can reuse it in other applications.

The Swift Package Manager will manage module or application dependencies and will automate the process of downloading, compiling, and linking these dependencies. This allows us to concentrate on our code rather than figuring out how to write complex build scripts to compile our applications or modules.

A package consists of the Swift source files and a manifest file called Package.swift. The manifest file defines the package's name and contains instructions on how to build the package. We can customize the manifest file to declare build targets or dependencies, include or exclude source files, and specify build configurations for the module or application.

Let's look at how we can use the Package Manager to create a simple application. Firstly, we need to decide how we will name the application. For our example, we will use the name PMExample. Let's create a directory with that name and then change to that directory:

mkdir PMExample
cd PMExample

Now, we need to build the framework that the Package Manager needs. To do this, we will run the following command:

swift package init

This command will create both the Sources and Tests directory. It will also create a number of files including the Package.swift manifest file and also a file in the Sources directory with the same name as your application. If you look at the Package.swift manifest file, it should look as follows:

import PackageDescription 
let package = Package( name: "PMExample" ) 

This is the most basic manifest file, and it simply defines the name for the package. We will look at this manifest file in greater depth in Chapter 13, Using C Libraries with Swift.

Now, let's look at the PMExample.swift that was created for us in the Sources directory. It should contain code similar to the following code:

struct PMExample {       
   var text = "Hello, World!" 
} 

Let's add some code to this file, so it contains the following code:

struct PMExample {       
   var text = "Hello, World!" 
   func sayHello() { 
      print(text) 
   } 
} 

All we did in this example was add a method that prints Hello, World! to the console.

Tip

Don't worry if you do not understand this code yet. We are looking to get you familiar with the Package Manager and the compiler, so you feel comfortable compiling the code examples in this book.

Now let's add the main.swift file to the Sources directory. This will be the entry point to our application. Add the following code to this file:

let example = PMExample() 
example.sayHello() 

Now, go back to the PMExample directory, and run the following command to build the PMExample application.

swift build

This will build the application. If all is well, we will have an executable application in the PMExample/.build/debug directory named PMExample.

Note that, in the PMExample.swift file, all of the code was contained in the PMExample structure, whereas the code in the main.swift file was top-level code. Remember what we noted earlier: the main.swift file is the only file that can contain top-level code.

We only scratched the surface of what the Package Manager can do in this section. We will look at it further in Chapter 13, Using C Libraries with Swift.

When all of the source code is in one file, it will be much easier to use the swift compiler to build your executable code; however, once your application grows past that single source file, I would recommend looking at the Package Manager to manage your dependencies and your builds.