Swift for Beginners
DEVELOP AND DESIGN
Boisy G. Pitre
PEACHPIT PRESS
WWW.PEACHPIT.COM
Swift for Beginners: Develop and Design
Boisy G. Pitre
Peachpit Press
www.peachpit.com
To report errors, please send a note to .
Peachpit Press is a division of Pearson Education.
Copyright 2015 by Boisy G. Pitre
Editor: Robyn G. Thomas
Copyeditor: Darren Meiss
Proofreader: Nancy Bell
Technical editor: Steve Phillips
Compositor: Danielle Foster
Indexer: Valerie Haynes Perry
Cover design: Aren Straiger
Notice of Rights
All 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 getting permission for reprints and excerpts, contact .
Notice of Liability
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of the book, neither the author nor Peachpit shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the instructions contained in this book or by the computer software and hardware products described in it.
Trademarks
Apple, Cocoa, Cocoa Touch, Objective-C, OS X, and Xcode are registered trademarks of Apple Inc., registered in the U.S. and other countries. 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 Peachpit was aware of a trademark claim, the designations appear as requested by the owner of the trademark. All other product names and services identified throughout this book are used in editorial fashion only and for the benefit of such companies with no intention of infringement of the trademark. No such use, or the use of any trade name, is intended to convey endorsement or other affiliation with this book.
ISBN-13: 978-0-134-04470-5
ISBN-10: 0-134-04470-3
9 8 7 6 5 4 3 2 1
Printed and bound in the United States of America
To the girls: Toni, Hope, Heidi, Lillian, Sophie, and Belle
Acknowledgments
When creating a book, it truly takes a village, and I could not have had better support from the staff at Peachpit Press. Many thanks go to executive editor, Cliff Colby at Peachpit for providing me the opportunity; editor Robyn Thomas for her immensely helpful and invaluable editing skills and for keeping the project on track; technical editor Steve Phillips who meticulously commented on drafts of the book for technical correctness and whose contributions undoubtedly made this work better.
During my writing, Ive drawn inspiration from the works of a number of friends who are authors in the iOS and Mac OS developer community: Chris Adamson, Bill Cheeseman, Bill Dudney, Daniel Steinberg, and Richard Warren.
I would be remiss if I didnt mention the MacTech Conference dynamic duo Ed Marczak and Neil Ticktin, as well as CocoaConf maestro Dave Klein, for the writing and speaking opportunities that they have provided me at those venues.
A tip of the hat to James Dempsey, whose band, the Breakpoints, and their excellent Backtrace album fueled several long writing and review sessions with their rollicking tunes. Java Square Caf in downtown Opelousas, Louisiana provided a great place to write as well as tasty lattes. Also, thanks to Dave et Rays Camp Jam/Supper Club and my friends there who served as inspiration for several of the coding examples I used.
Much appreciation goes to the minds at Apple for creating Swift, along with a host of great products over the years that have enhanced my own productivity and that of many, many others.
Finally, many thanks to my family, especially my wife, Toni, whose patience and encouragement while I worked on this book was abundant.
About the Author
Boisy G. Pitre is Affectivas Mobile Visionary and lead iOS developer, where his work has led to the creation of the first mobile SDK for delivering emotions to mobile devices for the leading emotion technology company and spin-off of the MIT Media Lab. Prior to that he was a member of the Mac Products Group at Nuance Communications where he worked with a team of developers on Dragon Dictate.
He also owns Tee-Boy, a software company focusing on Mac and iOS applications for the weather and data acquisition markets, and has authored the monthly Developer to Developer column in MacTech Magazine.
Along with Bill Loguidice, Boisy co-authored the book CoCo: The Colorful History of Tandys Underdog Computer (2013), published by Taylor & Francis.
Boisy holds a Master of Science in Computer Science from the University of Louisiana at Lafayette, and resides in the quiet countryside of Prairie Ronde, Louisiana. Besides Mac and iOS development, his hobbies and interests include retro-computing, ham radio, vending machine and arcade game restoration, farming, and playing the French music of South Louisiana.
Contents
Introduction
Welcome to Swift for Beginners! Swift is Apples 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 careverybody wants to see it up close, kick the tires, and take it for a spin down the road. Thats probably why youre reading this bookyouve heard about Swift and decided to see what all the fuss is about.
The notion that Swift is an easy language to use and learn certainly has merit, especially when compared to the capable but harder-to-learn programming language its replacing: Objective-C. Apple has long used Objective-C as its language of choice for developing software on its platforms, but that is changing with the introduction of Swift.
Not only is Swift easy to learn, its extremely powerful. Youll get a taste of some of that power here in this book.
Who Is This Book For?
This book was written for the beginner in mind. In a sense, were all beginners with Swift because its such a new language. However, many will want to learn Swift as a first or second computer language, many of whom havent had any exposure to Objective-C or related languages, C and C++.
Ideally, the reader will have some understanding and experience with a computer language; even so, the book is styled to appeal to the neophyte who is sufficiently motivated to learn. More experienced developers will probably find the first few chapters to be review material and light reading because the concepts are ubiquitous among many computer languages but nonetheless important to introduce Swift to the beginner.
No matter your skill level or prior experience, Swift for Beginners will appeal to anyone who wants to learn about Swift.
How to Use This Book
Like other books of its kind, Swift for Beginners is best read from start to finish. The material in subsequent chapters tends to build on the knowledge attained from previous ones. However, with few exceptions, code examples are confined to a single chapter.
The book is sized to provide a good amount of material, but not so much as to overwhelm the reader. Interspersed between the text are a copious number of screenshots to guide the beginner through the ins and outs of Swift as well as the Xcode tool chain.