Book Image

Swift Essentials

By : Alex Blewitt, Bandlem Limited
Book Image

Swift Essentials

By: Alex Blewitt, Bandlem Limited

Overview of this book

Table of Contents (16 chapters)
Swift Essentials
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

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 to interactively testing graphical content in the Playground editor, the Swift language and syntax is introduced by examples.

The book also introduces end-to-end iOS application development by showing you 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.

What this book covers

Chapter 1, Exploring Swift, presents the Swift read-evaluate-print-loop (REPL) and introduces the Swift language through examples on standard data types, functions, and looping.

Chapter 2, Playing with Swift, demonstrates Swift Playgrounds as a means to interactively play with the Swift code and obtain graphical results. It also introduces the playground format and shows how playgrounds can be created automatically from Markdown and AsciiDoc files.

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

Chapter 4, Storyboard Applications with Swift and iOS, introduces the concept of Storyboards as a means to create a multiscreen iOS application and shows how views in 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, 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.

Appendix provides additional references and resources to continue learning about Swift.

What you need for this book

The exercises in this book are written and tested for Swift 1.1, which is bundled with Xcode 6.1. To run the exercises, you need to have a Mac OS X computer running 10.9 or above with Xcode 6.1 or higher. 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 might affect the book's content.

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 https://developer.apple.com/xcode/downloads/, which is referenced from the iOS Developer Center at https://developer.apple.com/devcenter/ios/.

Once Xcode is installed, it can be launched from /Applications/Xcode.app or from Finder. To run the command-line based exercises, Terminal can be launched from /Applications/Utilities/Terminal.app, 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 that comes bundled with Xcode. It is not necessary to have an iOS device to write or test code. However, if you want to run the code on an iOS device, then you need to join the iOS developer program. More information is available at https://developer.apple.com/programs/ios/.

Who this book is for

This book is aimed at developers who are interested in learning the Swift programming language and how to write iOS applications using Swift. No prior programming experience for iOS is assumed, although a basic level of programming experience in a dynamically or statically typed programming language is expected. It is assumed that you are familiar with navigating and using Mac OS X and in the cases where Terminal commands are required, you have experience of simple shell commands or can pick them up quickly from the examples given.

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

The sources are provided in a GitHub repository at https://github.com/alblue/com.packtpub.swift.essentials/ and can be used to switch between the content of chapters using the tags in the repository. A knowledge of Git is helpful if you want to navigate between different versions; alternatively, the web-based interface at GitHub can be used instead. It is highly recommended that you become familiar with Git, as it is the standard version control system for Xcode and the de facto standard for open source projects. You are invited to read the Git topics at the author's blog, http://alblue.bandlem.com/Tag/git/, if you are unfamiliar and interested in learning more.

Trademarks

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.

Conventions

In this book, you will find a number of styles of text 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 include other contexts through the use of the include directive."

A block of code is set as follows:

@IBInspectable var progressAmount: CGFloat = 0.5 {
  didSet {
    setNeedsLayout()
  }
}

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

case "entry":
inEntry = true
case "link":
link = attributes.objectForKey("href") as String?
default break;

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

# cp /usr/src/asterisk-addons/configs/cdr_mysql.conf.sample
     /etc/asterisk/cdr_mysql.conf

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Clicking the Next button moves you to the next screen."

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 may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via 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 on 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 all Packt books you have purchased 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.

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 would 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 on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright 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.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.