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

Swift compiler


When we run our code using the swift command, or the Swift REPL tool, we are running the code as we would run a typical interpreted script such as a Bourne Again SHell (BASH) or Python script. This is really nice if we want to use Swift as a scripting language but if we want to build applications with Swift, we will need to compile our code into an executable form. To do this, we can use the Swift compiler.

Let's see how we would use the Swift compiler to build the Hello World example that we created earlier. If you recall from that example, we had to create a main.swift file. While using a main.swift file is not required for an application that only contains a single source file, it is required for applications that contain multiple source files. The Swift compiler will look for the main.swift and use it as the entry point for the application, similar to how a C compiler uses the main() function.

Tip

It is a good habit, when all of our code is contained in one file, to name that file main.swift.

To build the Hello World application that we created earlier, we would need to run the following command in the same directory that the main.swift file is in:

swiftc main.swift

This command should only take a second or so to run. Once it is completed, we should have an executable file named main. If your application contains multiple files, you can list them one by one as follows:

swiftc main.swift file1.swift file2.swift file3.swift

If you want to change the name of the output file, you can use the -o option as follows:

swiftc main.swift file1.swift file2.swift -o myexecutable

There are a number of command line options that you can use with the swift compiler. However, if you find that you are using a lot of command line options and making a complex compiler statement, you may want to look at the Swift Package Manager.