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

Preface

In December 2015, Apple released Swift as an open source project on its GitHub page. With this release, Apple also released a version of Swift for Linux. Swift is a general-purpose programming language that takes a modern approach to development.

What this book covers

Chapter 1, Taking the First Steps with Swift, gives a brief introduction to the Swift language. We will also cover the basic Swift language syntax and discuss proper language styles.

Chapter 2, Learning About Variables, Constants, Strings, and Operators, shows how to use variables and constants in Swift. We will also look at the various data types and how to use operators in Swift.

Chapter 3, Using Swift Collections and the Tuple Type, looks at how we can use the Swift collection types to store related data. These collection types are the dictionary, array and set types.

Chapter 4, Control Flow and Functions, covers control flow and functions in Swift. It is essential to understand the concepts in this chapter before going on. Every application that we write, beyond the simple Hello World applications, will rely very heavily on the control flow statements and functions.

Chapter 5, Classes and Structures, dedicates itself to Swift's classes and structures. We'll look at what makes them similar and what makes them different. We'll also look at access controls and object-oriented design. We'll close this chapter out by looking at memory management in Swift.

Chapter 6, Using Protocols and Protocol Extensions, covers both protocols and protocol extensions in detail since protocols are very important to the Swift language. Having a solid understanding of them will help us write flexible and reusable code.

Chapter 7, Protocol-Oriented Design, covers the best practices of protocol-oriented Design with Swift. It will be a brief overview of what is covered in my Swift 3 Protocol-Oriented Programming book.

Chapter 8, Writing Safer Code with Error Handling, looks at Swift's error-handling features. This feature is really important for writing safe code. While we are not required to use this feature in our custom types however it does give us a uniform manner to handle and respond to the error. Apple has also started to use this error handling in their frameworks. It is recommended that we use error handling in our code.

Chapter 9, Custom Subscripting, discusses how we can use custom subscripts in our classes, structures, and enumerations. Subscripts in Swift can be used to access elements in a collection. We can also define custom subscripts for our classes, structures, and enumerations.

Chapter 10, Using Optional Types, explains what optional types really are, what the various ways to unwrap them are, and optional chaining. For a developer who is just learning Swift, optional types can be one of the most confusing items to learn.

Chapter 11, Working with Generics, explains how Swift implements generics. Generics allow us to write very flexible and reusable code that avoids duplication.

Chapter 12, Working with Closures, teaches us how to define and use closures in our code. Closures in Swift are similar to blocks in Objective-C except that they have a much cleaner and easier to use syntax. We will conclude this chapter with a section on how to avoid strong reference cycles with closures.

Chapter 13, Using C Libraries with Swift, explains how we can link and use standard C libraries with our Swift applications. This gives Swift developers access to all of the same libraries that Linux C developers have access to.

Chapter 14, Concurrency and Parallelism in Swift, shows how to use Grand Central Dispatch (GCD) to add concurrency and parallelism to our applications. Understanding and knowing how to add concurrency and parallelism to our apps can significantly enhance the user experience.

Chapter 15, Swift's Core Libraries, explains how to use the Swift core libraries, including reading/writing files, network primitives, and JSON parsing.

Chapter 16, Swift on Single Board Computers, shows how we can use Swift to develop IoT devices and robots using a single board computer.

Chapter 17, Swift Formatting and Style Guide, defines a style guide for the Swift language that can be used as a template for enterprise developers who need to create a style guide, since most enterprises have style guides for the various languages that they develop in.

Chapter 18, Adopting Design Patterns in Swift, shows you how to implement some of the more common design patterns in Swift. A design pattern identifies a common software development problem and provides a strategy for dealing with it.

What you need for this book

To follow along with the samples in this book, the reader will need a computer that has Ubuntu 14.04, 15.10, 16.04, or 16.10 installed. To follow along with the samples in Chapter 16, Swift on Single Board Computers, the reader will need a BeagleBone Black or Green single board computer and the additional components mentioned in the chapter.

Who this book is for

Swift is a modern, fast, and safe open source language created by Apple. This book is for Linux developers who are interested in learning how to use Swift to quickly create exciting applications for the desktop, server, and embedded Linux platforms. This book will be written for developers who learn best by working with code, as every topic discussed will be reinforced with code samples.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can change the value of the highTemperture variable without an error because it is a variable"

A block of code is set as follows:

var x = 3.14      // Double type 
var y = "Hello"   // String type 
var z = true      // Boolean type

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

<head> 
<script src="d3.js" charset="utf-8"></script>  
  <meta charset="utf-8"> 
  <meta name="viewport" content="width=device-width"> 
  <title>JS Bin</title> 
</head>

Any command-line input or output is written as follows:

sudo mkdir swift

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "In order to download new modules, we will go to FilesSettings | Project Name | Project Interpreter."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Mastering-Swift-3-Linux. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/MasteringSwift3Linux_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.