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 and the Swift REPL


There are a couple of ways in which you can quickly experiment with Swift code. The first is the Swift interactive Read Evaluate Print Loop (REPL). The REPL is a command line tool that evaluates our code as we write it. Developers who are used to interpreting languages will be comfortable using this tool.

To start the REPL, you will need to open a terminal prompt and enter the following command:

swift

You will be greeted with a prompt similar to the following:

Welcome to Swift version 3.0 ({your-swift-version}). Type :help for
    assistance
1>

From here, we can type in any Swift statement and hit Enter. The REPL will immediately execute our code. The following is an example:

1>  var x = 10
x: Int = 10
2>  x += 5
3>  print(x)
15

To exit the REPL, type the following command:

:quit

We can also quickly test a Swift source file using the swift command. To try this out, create a file named Hello.swift, and put the following code in it:

print("Hello") 

Now let's run the following command to execute this source file:

swift Hello.swift 

You should see the message Hello printed to the console. The REPL and Swift commands are extremely powerful tools, and you can do a lot more than just prototyping new code. If you find these tools useful, I would recommend spending time learning about the various advanced features.