Swift For Beginners

Transcription

Swift forBeginnersDEVELOP AND DESIGNSECOND EDITIONBoisy G. Pitre

Swift forBeginnersSECOND EDITIONDEVELOP AND DESIGNBoisy G. PitrePEACHPIT PRESSWWW.PEACHPIT.COM

Swift for Beginners: Develop and Design, Second EditionBoisy G. PitrePeachpit Presswww.peachpit.comTo report errors, please send a note to errata@peachpit.com.Peachpit Press is a division of Pearson Education.Copyright 2016 by Boisy G. PitreEditor: Connie Jeung-MillsProduction editor: David Van NessDevelopment editor: Robyn G. ThomasCopyeditor and proofreader: Scout FestaTechnical editor: Steve PhillipsCompositor: Danielle FosterIndexer: Valerie Haynes PerryCover design: Aren StraigerInterior design: Mimi HeftNotice of RightsAll rights reserved. No part of this book may be reproduced or transmitted in any form by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. For information on gettingpermission for reprints and excerpts, contact permissions@peachpit.com.Notice of LiabilityThe information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken inthe preparation of the book, neither the author nor Peachpit shall have any liability to any person or entity with respect toany loss or damage caused or alleged to be caused directly or indirectly by the instructions contained in this book or by thecomputer software and hardware products described in it.TrademarksApple, Cocoa, Cocoa Touch, Objective-C, OS X, Swift, and Xcode are registered trademarks of Apple Inc., registered in theU.S. and other countries. Many of the designations used by manufacturers and sellers to distinguish their products areclaimed as trademarks. Where those designations appear in this book, and Peachpit was aware of a trademark claim, thedesignations appear as requested by the owner of the trademark. All other product names and services identified throughoutthis book are used in editorial fashion only and for the benefit of such companies with no intention of infringement of thetrademark. No such use, or the use of any trade name, is intended to convey endorsement or other affiliation with this book.ISBN-13: nted and bound in the United States of America

To the girls: Toni, Hope, Heidi, Lillian, Sophie, and Belle

This page intentionally left blank

ACKNOWLEDGMENTSWhen Peachpit’s executive editor Cliff Colby approached me about writing a second editionof Swift for Beginners, I readily agreed for several reasons. First, Apple has continuedto evolve the Swift language to the point where a book update was necessary—the firstedition was already outdated with respect to Swift 2 language enhancements and Xcodeimprovements. Second, I was eager to work with the same great team of people who werepart of the first edition.Shortly after the project started, Cliff left Peachpit and moved on to another adventure,but not before introducing me to Connie Jeung-Mills, who took over as executive editor.She brought together the original team from the first edition: Robyn Thomas as editor andSteve Phillips as technical editor. Rounding out the team was an addition, Scout Festa, whoprovided additional editorial support. Each one of them was indispensable and critical to theprocess, and I want to thank them for their assistance.On the technical side, I continue to draw inspiration from the works of a number offriends who are authors in the iOS and Mac OS developer community: Chris Adamson, BillCheeseman, James Dempsey, Bill Dudney, Daniel Steinberg, and Richard Warren. Thanks goto MacTech’s Ed Marczak and Neil Ticktin, as well as CocoaConf maestro Dave Klein, for thewriting and speaking opportunities that they have provided me at those venues. My friendsat Dave et Ray’s Camp Jam/Supper Club always serve as inspiration for several of the codingexamples I used in this edition. I also would like to thank Troy Deville for contributing thesource code for his game Downhill Challenge.Thanks also go to the minds at Apple for creating and enhancing Swift, currently inits second major release. The language has solidified since its introduction just over a yearago, and has already reached a popularity that is uncharacteristic for a computer languageso young.Lastly, my family and my wife, Toni, deserve special mention for the patience andencouragement they’ve shown while I worked on yet another book.Acknowledgmentsv

This page intentionally left blank

ABOUT THE AUTHORBoisy G. Pitre is Mobile Visionary and lead iOS developer at Affectiva, the leading emotiontechnology company and a spin-off of the MIT Media Lab. His work there has led to the creation of the first mobile SDK for delivering emotions to mobile devices. Prior to that he wasa member of the Mac Products Group at Nuance Communications, where he worked with ateam of developers on Dragon Dictate.He also owns Tee-Boy, a software company focusing on Mac and iOS applications forthe weather and data-acquisition markets, and he has authored the monthly “Developer toDeveloper” column in MacTech magazine.Along with Bill Loguidice, Boisy co-authored the book CoCo: The Colorful History ofTandy’s Underdog Computer (2013), published by Taylor & Francis.Boisy holds a Master of Science in Computer Science from the University of Louisianaat Lafayette, is working toward his doctorate in computer science, and resides in the quietcountryside of Prairie Ronde, Louisiana. Besides Mac and iOS development, his hobbies andinterests include retro-computing, ham radio, vending machine and arcade game restoration, farming, and playing the French music of South Louisiana.About the Authorvii

This page intentionally left blank

CONTENTSIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviWelcome to Swift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviiiSection IChapter 1THE BASICS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2INTRODUCING SWIFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4Evolutionary Yet Revolutionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Preparing for Success . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Tools of the Trade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Interacting with Swift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Ready, Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Diving into Swift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Help and Quit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Hello, World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10The Power of Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Constants Are Consistent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13This Thing Called a Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Testing the Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16Can a Leopard Change Its Stripes? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16Being Explicit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Strings and Things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Stringing Things Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Characters Have Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20Math and More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22Mixing Numeric Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22Numeric Representations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23True or False . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24The Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24Printing Made Easy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26Using Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27Grouping Data with Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28Optionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Contentsix

Chapter 2WORKING WITH COLLECTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32The Candy Jar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34Birds of a Feather . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Extending the Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38Replacing and Removing Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39Inserting Values at a Specific Location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40Combining Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41The Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42Looking Up an Entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43Adding an Entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45Updating an Entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46Removing an Entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46Arrays of Arrays? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47Starting from Scratch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50The Empty Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50The Empty Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51Iterating Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52Array Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52Dictionary Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55Chapter 3TAKING CONTROL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56For What It’s Worth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58Counting on It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58Inclusive or Exclusive? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59For Old Time’s Sake . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Writing Shorthand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62It’s Time to Play . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63Making Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66The Decisiveness of “If” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66When One Choice Is Not Enough . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70Switching Things Around . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72While You Were Away . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75Inspecting Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77Give Me a Break! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81xContents

Chapter 4WRITING FUNCTIONS AND CLOSURES . . . . . . . . . . . . . . . . . . . . . . . . 82The Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84Coding the Function in Swift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84Exercising the Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86More Than Just Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87Parameters Ad Nauseam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89Functions Fly First Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92Throw Me a Function, Mister . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94A Function in a Function in a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96Default Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98What’s in a Name? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100When It’s Good Enough . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102To Use or Not to Use? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102Don’t Change My Parameters! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103The Ins and Outs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105Bringing Closure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106Summing It Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109Stay Classy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109Chapter 5ORGANIZING WITH CLASSES AND STRUCTURES . . . . . . . . . . . . . 110Objects Are Everywhere . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112Swift Objects Are Classy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113Knock, Knock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114Let There Be Objects! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115Opening and Closing the Door . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116Locking and Unlocking the Door . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117Examining the Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120Door Diversity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120Painting the Door . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124Modeling the Base Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125Creating the Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128Instantiating the Subclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130Convenience Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138Structural Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141Value Types vs. Reference Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143Looking Back, Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145Contentsxi

Chapter 6FORMALIZING WITH PROTOCOLS AND EXTENSIONS . . . . . . . . . 146Following Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148Class or Protocol? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148More Than Just Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151Adopting Multiple Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153Protocols Can Inherit, Too . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155Delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156Extending With Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159Extending Basic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161Using Closures in Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167Section IIChapter 7DEVELOPING WITH SWIFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168WORKING WITH XCODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170Xcode’s Pedigree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172Creating Your First Swift Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173Diving Down . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174Interacting with the Project Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176It’s Alive! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178Piquing Your Interest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178Making Room . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179Building the UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180Tidying Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182Class Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183Hooking It Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188You Made an App! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189Chapter 8MAKING A BETTER APP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190It’s the Little Things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192Show Me the Money . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192Remember the Optional? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195Unwrapping Optionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195Looking Better . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196Formatting: A Different Technique . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196Compounding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201Hooking Things Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202Testing Your Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205xiiContents

When Things Go Wrong . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205Where’s the Bug? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206At the Breaking Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206The Confounding Compound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210The Value of Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210The Unit Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211Crafting a Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211When Tests Fail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215Tests That Always Run . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215Wrapping Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217Chapter 9GOING MOBILE WITH SWIFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218In Your Pocket vs. on Your Desk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220How’s Your Memory? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220Thinking About Gameplay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221Designing the UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221Creating the Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222Building the User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224Creating the Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225Running in the Simulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227Setting Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228The Model-View-Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230Coding the Game . . . . . . . . . . . . . . . . . . . . . . . . .

Swift is Apple’s new language for developing apps for iOS and Mac OS, and it is destined to become the premier computer language in the mobile and desktop space. As a new computer language, Swift has the allure of a shiny new car— everybody wants to see it up close, kick the tires, a