Mastering Swift 3 - Linux
By Jon Hoffman
()
About this ebook
- Create robust applications by building a strong foundation in the Swift Language
- Utilize Swift 3 on the embedded Linux platform for IoT and Robotic projects
- Build more flexible and high-performing applications on desktop, server, and embedded Linux platforms
This book is for Linux developers who are interested in quickly learning how to use Swift to create exciting applications on Linux platforms.
Read more from Jon Hoffman
Mastering Swift 5 - Fifth Edition: Deep dive into the latest edition of the Swift programming language, 5th Edition Rating: 0 out of 5 stars0 ratingsiOS and OS X Network Programming Cookbook Rating: 0 out of 5 stars0 ratingsMastering Swift 3 Rating: 0 out of 5 stars0 ratingsMastering Swift Rating: 0 out of 5 stars0 ratingsSwift: Developing iOS Applications Rating: 0 out of 5 stars0 ratings
Related to Mastering Swift 3 - Linux
Related ebooks
Learning Concurrent Programming in Scala Rating: 0 out of 5 stars0 ratingsJava 9 with JShell Rating: 0 out of 5 stars0 ratingsD Cookbook Rating: 0 out of 5 stars0 ratingsLearning Concurrent Programming in Scala - Second Edition Rating: 0 out of 5 stars0 ratingsDrupal 8 Configuration Management Rating: 0 out of 5 stars0 ratingsLinux Programming Tools Unveiled Rating: 0 out of 5 stars0 ratingsUnix / Linux FAQ: with Tips to Face Interviews Rating: 0 out of 5 stars0 ratingsCentOS 8 Essentials: Learn to Install, Administer and Deploy CentOS 8 Systems Rating: 0 out of 5 stars0 ratingsWindows Server 2012 Hyper-V Installation and Configuration Guide Rating: 0 out of 5 stars0 ratingsCross-Platform Desktop Applications: Using Node, Electron, and NW.js Rating: 0 out of 5 stars0 ratingsImplementing Domain-Specific Languages with Xtext and Xtend Rating: 4 out of 5 stars4/5Beginning C: From Beginner to Pro Rating: 0 out of 5 stars0 ratingsPro SQL Server on Linux: Including Container-Based Deployment with Docker and Kubernetes Rating: 0 out of 5 stars0 ratingsJasmine Cookbook Rating: 5 out of 5 stars5/5REST API Development with Node.js: Manage and Understand the Full Capabilities of Successful REST Development Rating: 0 out of 5 stars0 ratingsSeven Deadliest Unified Communications Attacks Rating: 0 out of 5 stars0 ratingsLinux Kernel Development A Complete Guide - 2020 Edition Rating: 0 out of 5 stars0 ratingsPHP Programming Solutions Rating: 0 out of 5 stars0 ratingsRadioastronomical Methods of Antenna Measurements Rating: 0 out of 5 stars0 ratingsLinux+ Study Guide: Exam: XK0-001 Rating: 5 out of 5 stars5/5Java 8 to 21: Explore and work with the cutting-edge features of Java 21 (English Edition) Rating: 0 out of 5 stars0 ratingsXML Programming Bible Rating: 0 out of 5 stars0 ratingsPractical Git: Confident Git Through Practice Rating: 0 out of 5 stars0 ratingsLearning CoreOS Rating: 0 out of 5 stars0 ratingsDrupal Multimedia Rating: 4 out of 5 stars4/5Pro Spring Boot 2: An Authoritative Guide to Building Microservices, Web and Enterprise Applications, and Best Practices Rating: 0 out of 5 stars0 ratingsC# Deconstructed: Discover how C# works on the .NET Framework Rating: 0 out of 5 stars0 ratingsKubernetes Secrets Management Rating: 0 out of 5 stars0 ratings
Operating Systems For You
Hacking : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Ethical Hacking Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5The Mac Terminal Reference and Scripting Primer Rating: 4 out of 5 stars4/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Exploring Windows 11: The Illustrated, Practical Guide to Using Microsoft Windows Rating: 0 out of 5 stars0 ratingsPowerShell: A Comprehensive Guide to Windows PowerShell Rating: 4 out of 5 stars4/5Learn Windows PowerShell in a Month of Lunches Rating: 0 out of 5 stars0 ratingsNetworking for System Administrators: IT Mastery, #5 Rating: 5 out of 5 stars5/5CompTIA Linux+ Study Guide: Exam XK0-004 Rating: 0 out of 5 stars0 ratingsWindows 11 All-in-One For Dummies Rating: 5 out of 5 stars5/5Mastering Windows PowerShell Scripting Rating: 4 out of 5 stars4/5Make Your PC Stable and Fast: What Microsoft Forgot to Tell You Rating: 4 out of 5 stars4/5Bash Command Line Pro Tips Rating: 5 out of 5 stars5/5iPhone Unlocked Rating: 0 out of 5 stars0 ratingsWindows 11 For Dummies Rating: 0 out of 5 stars0 ratingsLinux Command Line and Shell Scripting Bible Rating: 3 out of 5 stars3/5Macs All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsHacking for Beginners: Mastery Guide to Learn and Practice the Basics of Computer and Cyber Security Rating: 0 out of 5 stars0 ratingsWindows 10 All-In-One For Dummies Rating: 4 out of 5 stars4/5Linux for Beginners: Linux Command Line, Linux Programming and Linux Operating System Rating: 4 out of 5 stars4/5Windows Registry Forensics: Advanced Digital Forensic Analysis of the Windows Registry Rating: 4 out of 5 stars4/5OneNote: The Ultimate Guide on How to Use Microsoft OneNote for Getting Things Done Rating: 1 out of 5 stars1/5The Windows Command Line Beginner's Guide: Second Edition Rating: 4 out of 5 stars4/5Raspberry Pi Cookbook for Python Programmers Rating: 0 out of 5 stars0 ratingsDarknet Rating: 4 out of 5 stars4/5The Linux Command Line Beginner's Guide Rating: 4 out of 5 stars4/5
Reviews for Mastering Swift 3 - Linux
0 ratings0 reviews
Book preview
Mastering Swift 3 - Linux - Jon Hoffman
Table of Contents
Mastering Swift 3 - Linux
Credits
About the Author
About the Reviewers
www.PacktPub.com
Why subscribe?
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Taking the First Steps with Swift
What is Swift?
Swift's features
Installing Swift 3 for Linux
Swift language syntax
Comments
Semicolons
Parentheses
Curly brackets
Assignment operators do not return a value
Spaces are optional in conditional and assignment statements
Hello World
Executing Swift code
Swift and the Swift REPL
Swift compiler
Using the Swift Package Manger
Editors for Swift
Summary
2. Learning About Variables, Constants, Strings, and Operators
Constants and variables
Defining constants and variables
Type safety
Type inference
Explicit types
Numeric types
Integers
Floating point
The Boolean type
The String type
Optional variables
Enumerations
Operators
The assignment operator
Comparison operators
Arithmetic operators
The remainder operator
Compound assignment operators
The ternary conditional operator
The logical NOT operator
The logical AND operator
The logical OR operator
Summary
3. Using Swift Collections and the Tuple Type
Swift collection types
Mutability
Arrays
Creating and initializing arrays
Accessing the array elements
Counting the elements of an array
Is the array empty?
Appending to an array
Inserting a value into an array
Replacing elements in an array
Removing elements from an array
Adding two arrays
Reversing an array
Retrieving a subarray from an array
Making bulk changes to an array
Algorithms for arrays
Sort
Sorted
Filter
Map
forEach
Iterating over an array
Dictionaries
Creating and initializing dictionaries
Accessing dictionary values
Counting key or values in a dictionary
Is the dictionary empty?
Updating the value of a key
Adding a key-value pair
Removing a key-value pair
Set
Initializing a set
Inserting items into a set
The number of items in a set
Checking whether a set contains an item
Iterating over a set
Removing items in a set
Set operations
Tuples
Summary
4. Control Flow and Functions
What we have learned so far
Curly brackets
Parentheses
Control flow
Conditional statements
The if statement
Conditional code execution with the if...else statement
The for loop
Using the for...in loop
The while loop
Using the while loop
Using the repeat...while loop
The switch statement
Using case and where statements with conditional statements
Filtering with the where statement
Filtering with the for…case statement
Using the if...case statement
Control transfer statements
The continue statement
The break statement
The fallthrough statement
The guard statement
Functions
Using a single-parameter function
Using a multi-parameter function
Defining a parameter's default values
Returning multiple values from a function
Returning optional values
Adding external parameter names
Using variadic parameters
Inout parameters
Nesting functions
Putting it all together
Summary
5. Classes and Structures
What are classes and structures?
Similarities between classes and structures
Differences between classes and structures
Value versus reference types
Creating a class or structure
Properties
Stored properties
Computed properties
Property observers
Methods
Custom initializers
Internal and external parameter names
Failable initializers
Inheritance
Overriding methods and properties
Overriding methods
Overriding properties
Preventing overrides
Protocols
Protocol syntax
Property requirements
Method requirements
Extensions
Memory management
How ARC works
Strong reference cycles
Summary
6. Using Protocols and Protocol Extensions
Protocols as types
Polymorphism with protocols
Type casting with protocols
Protocol extensions
Summary
7. Protocol-Oriented Design
Requirements
Object-oriented design
Protocol-oriented design
Protocol inheritance
Protocol composition
Animal - protocol-oriented design
Using the where statement with protocols
Summary
8. Writing Safer Code with Error Handling
Error handling prior to Swift 2.0
Native error handling
Representing errors
Throwing errors
Catching errors
Summary
9. Custom Subscripting
Introducing subscripts
Subscripts with Swift arrays
Read and write custom subscripts
Read-only custom subscripts
Calculated subscripts
Subscript values
External names for subscripts
Multidimensional subscripts
When not to use a custom subscript
Summary
10. Using Optional Types
Introducing optionals
The need for optional types in Swift
Defining an optional
Using optionals
Forced unwrapping optionals
Optional binding
Returning optionals from functions, methods, and subscripts
Using optionals as a parameter in a function or method
Optional types with tuples
Optional chaining
The nil coalescing operator
Summary
11. Working with Generics
An introduction to generics
Generic functions
Generic types
Associated types
Summary
12. Working with Closures
An introduction to closures
Simple closures
Shorthand syntax for closures
Using closures with Swift's array algorithms
Changing functionality
Selecting a closure based on results
Creating strong reference cycles with closures
Summary
13. Using C Libraries with Swift
Modules
Creating a custom module
Creating the Cpcap module
Using the Cpcap module
Linux man pages
Summary
14. Concurrency and Parallelism in Swift
Concurrency and parallelism
GCD
Helper functions
Creating queues
Creating and using a concurrent queue
Creating and using a serial queue
async versus sync
Executing code on the main queue function
Using asyncAfter
Summary
15. Swifts Core Libraries
Apple's URL loading system
URLSession
URLSessionConfiguration
URLSessionTask
URL
URLRequest
HTTPURLResponse
REST web services
Making an HTTP GET request
Making an HTTP POST request
Formatter
DateFormatter
NumberFormatter
FileManager
Summary
16. Swift on Single Board Computers
The BeagleBone Black
BeagleBone Black's expansion headers
Installing Swift
SwiftyBones3
Sources directory
SwiftyBuild
Blinking an LED with the SBDigitalGPIO type
TMP36 temperature sensor with the SBAnalog type
Autonomous robot
Summary
17. Swift Formatting and Style Guide
What is a programming style guide?
Your style guide
Do not use semicolons at the end of statements
Do not use parentheses for conditional statements
Naming
Custom types
Functions and methods
Constants and variables
Indenting
Comments
Using the self keyword
Constants and variables
Optional types
Use optional binding
Use optional chaining over optional binding for multiple unwrapping
Use type inference
Use shorthand declaration for collections
Use switch rather than multiple if statements
Don't leave commented-out code in your application
Summary
18. Adopting Design Patterns in Swift
What are design patterns?
Creational patterns
The singleton design pattern
Understanding the problem
Understanding the solution
Implementing the singleton pattern
The builder design pattern
Understanding the problem
Understanding the solution
Implementing the builder pattern
Structural design patterns
The bridge pattern
Understanding the problem
Understanding the solution
Implementing the bridge pattern
The façade pattern
Understanding the problem
Understanding the solution
Implementing the façade pattern
The proxy design pattern
Understanding the problem
Understanding the solution
Implementing the proxy pattern
Behavioral design patterns
The command design pattern
Understanding the problem
Understanding the solution
Implementing the command pattern
The strategy pattern
Understanding the problem
Understanding the solution
Implementing the strategy pattern
Summary
Mastering Swift 3 - Linux
Mastering Swift 3 - Linux
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: January 2017
Production reference: 1281216
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78646-141-4
www.packtpub.com
Credits
About the Author
Jon Hoffman has over 20 years of experience in the field of information technology. Over these 20 years, Jon has worked in the areas of system administration, network administration, network security, application development, and architecture. Currently, Jon works as a senior software engineer for Syn-Tech Systems.
Jon has developed extensively for the iOS platform since 2008. This includes several apps that he has published in the App Store, apps that he has written for third parties, and numerous enterprise applications. What really drives Jon is the challenges that the field of information technology provides and there is nothing more exhilarating to him than overcoming a challenge.
You can follow Jon on his blogs: http://masteringswift.blogspot.com and http://myroboticadventure.blogspot.com.
Some of Jon’s other interests are watching baseball (Go Sox) and basketball (Go Celtics). Jon also really enjoys Tae Kwon Do where he and his oldest daughter Kailey earned their black belts together early in 2014, Kim (his wife) earned her black belt in December 2014, and his youngest daughter is currently working towards her black belt.
I would like to thank my wonderful wife, Kim, without whose support, encouragement, patience, and understanding, this book would have never been written. I would also like to thank my two wonderful daughters, Kailey and Kara, who have both been my inspiration and driving force since the days they were born.
About the Reviewers
Andrea Prearo is a software engineer with over 15 years of experience.
He is originally from Italy, and after a decade of writing software in C/C++ and C#, he moved to the Bay Area in 2011 to start developing mobile apps.
In the last few years, he has been focusing on Swift, Objective-C, iOS, and microservices, with some short explorations of the Android platform.
Currently, he is a member of the iOS development team at Capital One, working on the company’s flagship mobile banking app.
His interests include reading books, watching movies, and hiking. From time to time, he also blogs about tech on Medium: https://medium.com/@andrea.prearo.
I would like to thank my wonderful wife, Nicole, for her never-ending support in all my endeavors.
Doug Sparling works as a technical architect and software developer for Andrews McMeel Universal, a publishing and syndication company in Kansas City, MO. At AMU, he uses Go for web services, Python for backend services, and Ruby on Rails and WordPress for website development, and Objective-C, Swift, and Java for native iOS and Android development. AMU’s sites include www.gocomics.com, www.uexpress.com, www.puzzlesociety.com, and dilbert.com.
He also was the co-author of a Perl book, Instant Perl Modules for McGraw-Hill and a reviewer for other Packt Publishing books, including Swift Data Structure and Algorithms, jQuery 2.0 Animation Techniques: Beginner’s Guide, and WordPress Web Application Development. Doug has also played various roles for Manning Publications as a reviewer, technical development editor, and proofer, working on books such as Go in Action, The Well-Grounded Rubyist 2nd Edition, iOS Development with Swift, and Programming for Musicians and Digital Artists.
www.PacktPub.com
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
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:
utf-8>
viewport content=width=device-width
>
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 Files | Settings | 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 feedback@packtpub.com, 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:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the SUPPORT tab at the top.
Click on Code Downloads & Errata.
Enter the name of the book in the Search box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
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 copyright@packtpub.com 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 questions@packtpub.com, and we will do our best to address the problem.
Chapter 1. Taking the First Steps with Swift
Ever since I was 12 years old and wrote my first program in the BASIC language, programming has been a passion for me. Even as programming became my career, it always remained more of a passion than a job, but over the past few years, that passion has waned. I was unsure why I was losing that passion. I attempted to recapture it with some of my side projects, but nothing really brought back the excitement that I used to have. Then, something wonderful happened! Apple announced Swift, which is an exciting and progressive language that has brought a lot of that passion back and made programming fun for me again. Now that Apple has released Swift for Linux, I can use it for most of my projects.
In this chapter, you will learn the following topics:
What is Swift?
Some of the features of Swift
How to get started with Swift
What are the basic syntaxes of the Swift language?
What is Swift?
Swift is Apple's new programming language that was introduced at the Worldwide Developers Conference (WWDC) in 2014, alongside the integrated development environment Xcode 6 and iOS 8. Swift was arguably the most significant announcement at WWDC 2014, and very few people, including Apple insiders, were aware of the project's existence prior to it being announced. In this initial release of Swift it could only be used to develop applications for the Apple platforms.
It was amazing, even by Apple's standards, that they were able to keep Swift a secret for as long as they did, and no one suspected that they were going to announce a new development language. At WWDC 2015, Apple made another big splash when they announced Xcode 7 and Swift 2. Swift 2 was a major enhancement to the language. During that conference, Chris Lattner said that a lot of enhancements were based directly on feedback that Apple received from the development community. It was also announced at WWDC 2015 that Apple would be releasing Swift as open source, and they would be offering a port for Linux.
In December 2015, Apple officially released Swift as an open source project with the swift.org site dedicated to the open source Swift community. The Swift repository is located on Apple's github page (http://github.com/apple). The Swift evolution repository (https://github.com/apple/swift-evolution) tracks the evolution of the Swift language by documenting all of the proposed changes. You can also find a list of proposals that were accepted and rejected in this repository. If you are interested in understanding where Swift is heading, then you should check out this repository. It is interesting to note that Swift 3 contains several enhancements that were proposed by the community.
Swift 3 is a major enhancement to the language and is NOT source-compatible with previous releases. It contains fundamental changes to the language itself and to the Swift standard library. One of the main goals of Swift 3 is to be source-compatible across all platforms, so the code that we write for one platform will be compatible with all other platforms that Swift supports. This means that the code we develop for Mac OS will also work on Linux.
Development on Swift was started in 2010 by Chris Lattner. He implemented much of the basic language structure with only a few people being aware of its existence. It wasn't until late 2011 that other developers began to really contribute to Swift, and in July 2013, it became a major focus of the Apple Developer Tools group.
There are a lot of similarities between Swift and Objective-C. Swift adopts the readability of Objective-C's named parameters and its dynamic object model. When we refer to Swift as having a dynamic object model, we are referring to its ability to change at runtime. This includes adding new (custom) types and changing/extending the existing types.
While there are a lot of similarities between Swift and Objective-C, there are also significant differences between them as well. Swift's syntax and formatting are a lot closer to Python than Objective-C, but Apple did keep the curly braces. I know that Python people would disagree with me, and that is all right because we all have different opinions, but I like the curly braces. Swift actually makes the curly braces required for control statements such as if and while, which eliminates bugs, such as the goto fail in Apple's SSL library, which Apple patched in 2014.
Swift's features
When Apple said that Swift is Objective-C without the C, they were really only telling us half the story. Objective-C is a superset of C and provides object-oriented capabilities and a dynamic runtime to the C language. This means that, with Objective-C, Apple needed to maintain compatibility with C, which limited the enhancements it could make to the Objective-C language. As an example, Apple could not change how the switch statement functioned and still maintains the C compatibility.
Since Swift does not need to maintain the same C compatibility as Objective-C, Apple was free to add any feature/enhancement to the language. This allowed Apple to include the best features from many of today's most popular and modern languages, such as Objective-C, Python, Java, Ruby, C#, and Haskell.
The following chart shows a list of some of the most exciting enhancements that Swift includes:
Before we can look at the Swift language itself, we need to download and install it. Let's look at how we can install Swift.
Installing Swift 3 for Linux
The following instructions were current when this book was written and works for both Ubuntu 15.10 and Ubuntu 16.04 LTS. The most up-to-date installation instructions can be found on the swift.org installation page: https://swift.org/getting-started/#installing-swift.
The first step in installing Swift is to install clang and configure it. The following are the commands:
sudo apt-get update sudo apt-get install clang-3.6
sudo update-alternatives -install /usr/bin/clang clang
/usr/bin/clang-3.6 100
sudo update-alternatives -install /usr/bin/clang++ clang++
/usr/bin/clang++-3.6 100
If you forget to do the update-alternatives commands, you will receive an error: invalid inferred toolchain error when you try to build a package. Forgetting to run the update-alternatives commands is a very common error.
Now that we have the correct clang installed, we need to download Swift. You can download the latest version from the swift.org download page. The link for this page is: https://swift.org/download/.
Once you have downloaded Swift, you can install it in the location of your choice; however, you will want to avoid installing it off the root directory because that may overwrite the /usr directory. As a personal preference, I like to install Swift in the /opt directory. The following instructions will show how I install Swift.
Tip
Anytime you see {swift version}, substitute it for the version information of the Swift you are installing.
1. Go to the /opt directory:
cd /opt
2. Create a directory to hold this and all future Swift versions:
sudo mkdir swift
3. Change to the swift directory, and copy the downloaded swift file to this location:
cd swift sudo cp -R ~/Downloads/swift-{swift version}.tar.gz ./
4. Gunzip and untar the swift file:
tar -zxvf swift-{swift version}.tar.gz
5. The last command should have created a new directory that contains the Swift files. We will now want to create a symbolic link to this directory indicating that it is our current version of Swift:
sudo ln -s /opt/swift-{swift version} swift-current
6. All of the files in the swift directory are owned by the root user. If you attempt to build a Swift package with the package manager, while all of the files are owned by root, you will receive a permission denied error. The easiest way to fix this is to change the ownership of the Swift files to your user. If you want to give permission for multiple users to use Swift, you can add write permissions for all users. Instructions for both options are listed here and you only need to follow one of them:
//Change ownership of the Swift files sudo chown -R {username}:{username} swift-{swift version} //Alternatively change write permission sudo chmod -R +x swift-{swift-version}
7. Now, we need to add Swift to our path. To do