ebook img

Swift for Beginners: Develop and Design PDF

487 Pages·13.212 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Swift for Beginners: Develop and Design

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 [email protected]. 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 [email protected]. 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, I’ve 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 didn’t 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 Ray’s 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 Affectiva’s 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 Tandy’s 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 SECTION I THE BASICS CHAPTER 1 INTRODUCING SWIFT Evolutionary, yet Revolutionary Preparing for Success Tools of the Trade Interacting with Swift Ready, Set... Diving into Swift Help and Quit Hello World! The Power of Declaration Constants Are Consistent This Thing Called a Type Testing the Limits Can a Leopard Change Its Stripes? Being Explicit Strings and Things Stringing Things Together Characters Have Character Math and More Expressions Mixing Numeric Types Numeric Representations True or False The Result Printing Made Easy Using Aliases Grouping Data with Tuples Optionals Summary CHAPTER 2 WORKING WITH COLLECTIONS The Candy Jar Birds of a Feather... Extending the Array Replacing and Removing Values Inserting Values at a Specific Location Combining Arrays The Dictionary Looking Up an Entry Adding an Entry Updating an Entry Removing an Entry Arrays of Arrays? Starting from Scratch The Empty Array The Empty Dictionary Iterating Collections Array Iteration Dictionary Iteration Summary CHAPTER 3 TAKING CONTROL For What It’s Worth Counting On It Inclusive or Exclusive? For Old Time’s Sake Writing Shorthand It’s Time to Play Making Decisions The Decisiveness of “If” When One Choice Is Not Enough Switching Things Around While You Were Away... Inspecting Your Code Give Me a Break! Summary CHAPTER 4 WRITING FUNCTIONS AND CLOSURES The Function Coding the Function in Swift Exercising the Function More Than Just Numbers Parameters Ad Nauseam Functions Fly First Class Throw Me a Function, Mister A Function in a Function in a... Default Parameters What’s in a Name? When It’s Good Enough To Use or Not to Use? Don’t Change My Parameters! The Ins and Outs Bringing Closure Summing It Up Stay Classy CHAPTER 5 ORGANIZING WITH CLASSES AND STRUCTURES Objects Are Everywhere Swift Objects Are Classy Knock, Knock Let There Be Objects! Opening and Closing the Door Locking and Unlocking the Door Examining the Properties Door Diversity Painting the Door Inheritance Modeling the Base Class Creating the Subclasses Instantiating the Subclass Convenience Initializers Enumerations Structural Integrity Value Types vs. Reference Types Looking Back, Looking Ahead CHAPTER 6 FORMALIZING WITH PROTOCOLS AND EXTENSIONS Following Protocol Class or Protocol? More Than Just Methods Adopting Multiple Protocols Protocols Can Inherit, Too Delegation Extending with Extensions Extending Basic Types Using Closures in Extensions Summary SECTION II DEVELOPING WITH SWIFT CHAPTER 7 WORKING WITH XCODE Xcode’s Pedigree Creating Your First Swift Project Diving Down Interacting with the Project Window It’s Alive! Piquing Your Interest Making Room Building the UI Tidying Up Class Time Hooking It Up You Made an App! CHAPTER 8 MAKING A BETTER APP It’s the Little Things Show Me the Money Remember the Optional? Unwrapping Optionals Looking Better Formatting: A Different Technique Compounding Hooking Things Up Testing Your Work When Things Go Wrong Where’s the Bug? At the Breaking Point The Confounding Compound The Value of Testing The Unit Test

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.