Book Image

Swift Essentials - Second Edition

By : Alex Blewitt
Book Image

Swift Essentials - Second Edition

By: Alex Blewitt

Overview of this book

Swift was considered one of the biggest innovations last year, and certainly with Swift 2 announced at WWDC in 2015, this segment of the developer space will continue to be hot and dominating. This is a fast-paced guide to provide an overview of Swift programming and then walks you through in detail how to write iOS applications. Progress through chapters on custom views, networking, parsing and build a complete application as a Git repository, all by using Swift as the core language
Table of Contents (17 chapters)
Swift Essentials Second Edition
About the Author
About the Reviewer


Swift Essentials provides an overview of the Swift language and the tooling necessary to write iOS applications. From simple Swift commands on the command line using the open source version of Swift, to interactively testing graphical content on OS X with the Xcode Playground editor, Swift language and syntax is introduced by examples.

This book also introduces end-to-end iOS application development on OS X with Xcode by showing how a simple iOS application can be created, followed by how to use storyboards and custom views to build a more complex networked application.

The book concludes by providing a worked example from scratch that builds up a GitHub repository browser for iOS, along with an Apple Watch application.

What this book covers

Chapter 1, Exploring Swift, presents the open source version of Swift with the Swift Read-Evaluate-Print-Loop (REPL) and introduces the Swift language through examples of standard data types, functions, and looping.

Chapter 2, Playing with Swift, demonstrates Swift Xcode Playgrounds as a means to interactively play with Swift code and see graphical results. It also introduces the playground format and shows how playgrounds can be documented.

Chapter 3, Creating an iOS Swift App, shows how to create and test an iOS application built in Swift using Xcode, along with an overview of Swift classes, protocols, and enums.

Chapter 4, Storyboard Applications with Swift and iOS, introduces the concept of Storyboards as a means of creating a multiscreen iOS application and shows how views in the Interface Builder can be wired to Swift outlets and actions.

Chapter 5, Creating Custom Views in Swift, covers custom views in Swift using custom table views, laying out nested views, and drawing custom graphics and layered animations.

Chapter 6, Parsing Networked Data, demonstrates how Swift can talk to networked services using both HTTP and custom stream-based protocols.

Chapter 7, Building a Repository Browser, uses the techniques described in this book to build a repository browser that can display information about users' GitHub repositories.

Chapter 8, Adding Watch Support, introduces the capabilities of the Apple Watch and shows how to build an extension for the iOS app to provide data directly on the watch.

The Appendix, References to Swift-related Websites, Blogs, and Notable Twitter Users, provides additional references and resources to continue learning about Swift.

What you need for this book

The exercises in this book were written and tested for Swift 2.1, which is bundled with Xcode 7.2, and verified against a development build of Swift 2.2. To experiment with Swift, you will need either a Mac OS X or Linux computer that meets the requirements shown at

To run the exercises involving Xcode in Chapters 2–8, you need to have a Mac OS X computer running 10.9 or above with Xcode 7.2 or above. If newer versions of Swift are released, check the book's GitHub repository or the book's errata page at Packtpub for details about any changes that may affect the book's content.


The Swift playground (described in Chapter 2, Playing with Swift) is only available as part of Xcode on OS X and is not part of the open source version of Swift.

Also, iOS and watchOS development (Chapters 3-8) is only possible on OS X with Xcode; it is not possible to create iOS or watchOS applications on other platforms. Most of the required libraries and modules for iOS development are not available as part of the open source version of Swift.

Xcode can be installed via the App Store as a free download; search for Xcode in the search box. Alternatively, Xcode can be downloaded from, which is referenced from the iOS Developer Center at

Once Xcode has been installed, it can be launched from /Applications/ or from Finder. To run the command line-based exercises, Terminal can be launched from /Applications/Utilities/, and if Xcode is installed successfully, swift can be launched by running xcrun swift.

The iOS applications can be developed and tested in the iOS simulator, which comes bundled with Xcode. It is not necessary to have an iOS device to write or test the code. If you want to run the code on your own iOS device, then you will need an Apple ID to sign in, but the application will be limited to directly connected devices. Similarly, the watch application can be tested in a local simulator or on a local device.

Publishing the application to the AppStore requires that you join the Apple Developer Program. More information is available at

Who this book is for

This book is aimed at developers who are interested in learning the Swift programming language, either using the open source version of Swift on Linux or the version bundled with Xcode on OS X. However, after Chapter 1, Exploring Swift, the remainder of the chapters use Xcode features or have iOS examples which can only be used on OS X with Xcode. These chapters show how to write iOS applications on OS X using Swift. No prior programming experience for iOS is assumed, though a basic level of programming experience in a dynamically or statically typed programming language is expected. The reader will be familiar with navigating and using Mac OS X and, in the cases where Terminal commands are required, the developer will have experience of simple shell commands or can pick it up quickly from the examples given.

Developers familiar with Objective-C will know many of the frameworks and libraries mentioned; however, existing knowledge of Objective-C and its frameworks is neither necessary nor assumed.

The sources are provided in a GitHub repository at, and they can be used to switch between the content of chapters using the tags in the repository. Knowledge of Git is helpful if you are wanting to navigate between different versions; alternatively, the web-based interface at GitHub may be used instead. It is highly recommended that the reader becomes familiar with Git as it is the standard version control system for Xcode and the de facto standard for open source projects. The reader is invited to read the Git topics at the author's blog if they are unfamiliar and interested in learning more.


GitHub is a trademark of GitHub Inc., and the examples in this book have not been endorsed, reviewed, or approved by GitHub Inc. Mac and OS X are trademarks of Apple Inc., registered in the U.S. and other countries. iOS is a trademark or registered trademark of Cisco in the U.S. and other countries and is used under license.


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: " "hello".hasPrefix("he") method compiles and runs successfully on OS X and iOS."

A block of code is set as follows:

> var shopping = [ "Milk", "Eggs", "Coffee", ]
shopping: [String] = 3 values {
  [0] = "Milk"
  [1] = "Eggs"
  [2] = "Coffee"

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

func setupView() {
  contentMode = .Redraw

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: "Xcode documentation can be searched by navigating to Help | Documentation and API Reference."


Warnings or important notes appear in a box like this.


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 , 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

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 from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


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, 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 and enter the name of the book in the search field. The required information will appear under the Errata section.


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 with a link to the suspected pirated material.

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


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