Swift Programming: The Big Nerd Ranch Guide
by Matthew Mathias and John Gallagher
Copyright 2016 Big Nerd Ranch, LLC
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, contact
Big Nerd Ranch, LLC
200 Arizona Ave NE
Atlanta, GA 30307
(770) 817-6373
http://www.bignerdranch.com/
book-comments@bignerdranch.com
The 10-gallon hat with propeller logo is a trademark of Big Nerd Ranch, LLC.
Exclusive worldwide distribution of the English edition of this book by
Pearson Technology Group
800 East 96th Street
Indianapolis, IN 46240 USA
http://www.informit.com
The authors and publisher have taken care in writing and printing this book but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
ISBN-10 0134610695
ISBN-13 978-0134610696
Second edition, first printing, November 2016
Release E.2.1.1
Dedication
For my wife, who is smart, strong, and virtuous. And for my family, who has given me every opportunity to live a good life. |
M.M. |
For my wife and best friend; you are swonderful. And for my daughters, who bring me joy every day. |
J.G. |
Acknowledgments
We received a lot of help in writing this book. Without it, this book would not be what it is, and it may never even have happened. Thanks are due.
First, we need to say thank you to our colleagues at Big Nerd Ranch. Thank you to Aaron Hillegass for providing us with the opportunity to write this book. It has been immensely gratifying to learn and teach Swift. Big Nerd Ranch provided us with the time and space to work on this project. We hope that this book lives up to the trust and the support that we have received.
Particular thanks are also due to our colleagues in the Cocoa Pod at Big Nerd Ranch. Your careful teaching revealed many bugs in the text, and your thoughtful recommendations led to many improvements in our approach. Those of you who are not instructors helped to review the materials, vetted our approach, and provided countless suggestions that we never thought of. It is truly wonderful to have colleagues such as you. Thank you Pouria Almassi, Matt Bezark, Nate Chandler, Step Christopher, Kynerd Coleman, Matthew Compton, Joseph Dixon, Robert Edwards, Sean Farrell, Brian Hardy, Florian Harr, Tom Harrington, David House, Bolot Kerimbaev, Christian Keur, JJ Manton, Bill Monk, Chris Morris, Adam Preble, Scott Ritchie, Jeremy Sherman, Steve Sparks, Rod Strougo, TJ Usiyan, Zach Waldowski, Thomas Ward, and Mike Zornek.
Our colleagues in operations, marketing, and sales are instrumental. Classes would literally never be scheduled without their work. Thank you Shannon Coburn, Nicole Rej, Heather Sharpe, Tasha Schroeder, Jade Hill, Nicola Smith, Mat Jackson, Chris Kirksey, and Jon Malmgren for all of your hard work. We cannot do what you do.
Second, we need to acknowledge the many talented folks who worked on the book with us.
Elizabeth Holaday, our editor, helped refine the book, crystallize its strengths, and diminish its weaknesses.
Simone Payment and Anna Bentley, our copyeditors, found and corrected errors and ultimately made us look smarter than we are.
Ellie Volckhausen designed our cover; that skateboard looks pretty rad.
Chris Loper designed and produced the print book and the EPUB and Kindle versions.
Finally, thank you to our students. We learned with you and for you. Teaching is part of the greatest thing that we do, and it has been a pleasure working with you. We hope that the quality of this book matches your enthusiasm and determination.
Introduction
Learning Swift
Apples Worldwide Developers Conference is an annual landmark event for its developer community. It is a big deal every year, but 2014 was particularly special because Apple introduced Swift, an entirely new language for the development of iOS and OS X (now called macOS) applications.
As a relatively new language, Swift represents a fairly dramatic shift for macOS and iOS developers. More experienced iOS developers have something new to learn, and new developers cannot rely on a venerable community for tried and true answers and patterns. Naturally, this shift creates some uncertainty.
But this is also an exciting time to be a macOS and iOS developer. There is a lot to learn in a new language, and this is especially true for Swift. The language has evolved quite a bit since its beta release in the summer of 2014, and it continues to evolve.
We are all at the forefront of this languages development. As new features are added to Swift, its users can collaboratively determine its best practices. You can directly contribute to this conversation, and your work with this book will start you on your way to becoming a contributing member of the Swift community.
Why Swift?
If you have experience using Objective-C to develop for Apple platforms, you may be wondering why Apple released a new language. After all, developers had been producing high-quality apps for OS X and iOS for years. Apple had a few things in mind.
First, Objective-C is an older language. And while this is not always a problem, it leads to some difficulty in this case. The syntax of Objective-C was solidified prior to the rise of prominent scripting languages in the 1990s that popularized more streamlined and elegant syntax (e.g., JavaScript, Python, PHP, Ruby, and others). This makes Objective-C feel strange to most developers when they get started, so its syntax can be an impediment to developer productivity. Additionally, as an older language, Objective-C is missing many advancements developers in modern languages currently enjoy.
Also, Swift aims to be safe. Objective-C did not aim to be unsafe, but things have changed quite a bit since Objective-C was released in the 1980s. For example, the Swift compiler aims to minimize undefined behavior, which is intended to save the developer time debugging code that failed during the runtime of an application.
Another goal of Swift is to be a suitable replacement for the C family of languages (C, C++, and Objective-C). That means Swift has to be fast. Indeed, Swifts performance is comparable to these languages in most cases.
Swift gives you safety and performance all in a clean, modern syntax. The language is quite expressive; developers can write code that feels natural. This feature makes Swift a joy to write and easy to read, which makes it great for collaborating on larger projects.
Last, Apple wants Swift to be a general purpose programming language. This desire was reflected by the decision to open source Swift in December 2015. Open sourcing the language not only invites developer involvement to help the language progress, but also makes it easier for developers to port the language to other systems beyond macOS and iOS. Apple hopes that developers will use Swift to write apps for a variety of mobile and desktop platforms and to develop back-end web applications as well. Swift is intended to become a mainstream programming language that is the best solution for writing a variety of applications on a variety of systems.