Preface
Swift is Apple's new programming language that was introduced at the WWDC (Worldwide Developers Conference) 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.
Swift can be thought of as Objective-C reimagined using modern concepts and safe programming patterns. In Apple's own words, Swift is like "Objective-C without the C". Chris Lattner, the creator of Swift, said Swift took language ideas from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list.
In Apple's own words "Swift is a successor to the C and Objective-C languages"; therefore, it is imperative for iOS and OS X developers who want to keep their skills up to date to not only learn but also master the Swift programming language.
The first five chapters of this book will introduce the reader to the Swift programming language. These chapters will give the reader a solid understanding of the Swift programming language. The remainder of the book will cover more advance topics such as concurrency, network development, and design patterns and will help the reader master the swift programming language.
This book is written in an example-based approach where each topic covered is backed by examples written to reinforce the topic and to show how to implement it within the reader's code.
Since Swift is constantly changing and evolving, the author has started a blog at http://masteringswift.blogspot.com to keep readers up to date with what is new in Swift. The blog will also be used to enhance and expand on the material in the book.
What this book covers
Chapter 1, Taking the First Steps with Swift, introduces the reader to the Swift programming language and discusses what inspired Apple to create Swift. We also go over the basic syntax of Swift and how to use Playgrounds to experiment and test Swift code.
Chapter 2, Learning about Variables, Constants, Strings, and Operators, explains to the reader about variables and constants in Swift and when to use them. There are brief overviews of the most common variable types, with examples of how to use them. We conclude this chapter by showing examples of how to use the most common operators in the Swift language.
Chapter 3, Using Collections and Cocoa Data Types, introduces Swift's Array and Dictionary collection types and shows examples of how to use them. We will also show how to use the Cocoa and Foundation data types with Swift.
Chapter 4, Control Flow and Functions, explains how to use Swift's control flow statements. These include looping, conditional, and control transfer statements. The second half of the chapter is all about functions and how to define and use them.
Chapter 5, Classes and Structures, explains Swift's classes and structures in detail. We will look at what makes them similar and what makes them different. We will also look at access controls and object-oriented design. We will close this chapter out by looking at memory management in Swift.
Chapter 6, Working with XML and JSON Data, starts off by discussing what XML and JSON data is and their uses. We then show several examples of how to parse and build XML and JSON data using Apple's frameworks.
Chapter 7, Custom Subscripting, examines 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 8, Using Optional Type and Optional Chaining, looks at what optional types really are, various ways to unwrap them, and optional chaining. For a developer just learning Swift, optional types can be one of the most confusing items to learn.
Chapter 9, Working with Generics, allows us to write very flexible and reusable code that avoids duplication. In this chapter, we will examine how Swift implements generics. We will also look at the proper ways to use generics in our code.
Chapter 10, Working with Closures, examines how to define and use closures in our code. We will conclude this chapter with a section on how to avoid strong reference cycles with closures. Closures in Swift are similar to blocks in Objective-C, except that they have a much cleaner and easier-to-use syntax.
Chapter 11, Using Mix and Match, examines mix and match and demonstrates how we can include Swift code in our Objective-C projects and Objective-C code in our Swift projects. With all of the apps and frameworks written in Objective-C, it was important to allow Swift and Objective-C code to work together.
Chapter 12, Concurrency and Parallelism in Swift, shows how to use both Grand Central Dispatch (GCD) and Operation Queues 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 13, Swift Formatting and Style Guide, defines a style guide for the Swift language that can be a template for enterprise developers that need to create a style guide. Most enterprises have style guides for the various languages that they develop in.
Chapter 14, Network Development with Swift, looks at the Apple APIs to connect to remote severs and how to best use them. Network development can be both fun and challenging.
Chapter 15, Adopting Design Patterns in Swift, examines 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 examples in this book, the reader will need to have a Mac computer with OS X 10.10 or higher installed. They will also need to install Xcode version 6.3.1 or higher with Swift version 1.2 or higher.
Who this book is for
This book is intended for inpiduals looking for a book that not only gives them a solid introduction to the Swift programming language, but will also cover advanced topics such as ARC, Design Patterns, and Concurrency in depth. This book is written for the developer that learns best by looking at and working with code, because each concept covered in the book is backed by example code written to give the reader a good understanding of the current topic and to demonstrate how to properly implement it.
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: "To define a constant, you use the keyword let
and to define a variable you use the keyword var
."
A block of code is set as follows:
// Constants let freezingTemperatureOfWaterCelsius = 0 let speedOfLightKmSec = 300000 // Variables var currentTemperature = 22 var currentSpeed = 55
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: "As we can see in the Timeline sidebar, our application will print the message Hello World! to the console."
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 from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. 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.
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/2154OT_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.