ebook img

Swift Apprentice: Beginning Programming with Swift 3 PDF

383 Pages·2016·10.211 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 Apprentice: Beginning Programming with Swift 3

Swift Apprentice Swift Apprentice Janie Clayton, Alexis Gallagher, Matt Galloway, Eli Ganem, Erik Kerber, Ben Morrow, Cosmin Pupăză and Steven Van Impe Copyright ©2016 Razeware LLC. Notice of Rights All rights reserved. No part of this book or corresponding materials (such as text, images, or source code) may be reproduced or distributed by any means without prior written permission of the copyright owner. Notice of Liability This book and all corresponding materials (such as source code) are provided on an “as is” basis, without warranty of any kind, express of implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in action of contract, tort or otherwise, arising from, out of or in connection with the software or the use of other dealing in the software. Trademarks All trademarks and registered trademarks appearing in this book are the property of their own respective owners. raywenderlich.com 2 Swift Apprentice Dedications "Here's to the crazy ones. Except Josh. He knows what he did." — Janie Clayton "To my wife, and her remarkable patience in the face of the rationally inexplicable features of my writing process." — Alexis Gallagher "To my amazing family who keep putting up with me spending my spare hours writing books like this." — Matt Galloway "To the two loves of my life, Moriah and Lia." — Eli Ganem "To the Swift community, both old and new — and to Brigette, who puts up with my long hours at the keyboard to help make this book possible." — Erik Kerber "For MawMaw. A talented cook, a loving smooch, a worthy opponent in chicken foot; a home weaver. Her blessing abides beyond her time." — Ben Morrow raywenderlich.com 3 Swift Apprentice "To my Mum and Dad, for their total encouragement, entire trust, unconditional support and completely believing in me all the way with everything I do no matter what. To my grandmother, whom I miss a lot and do hope I make her proud with all the things I accomplish. To Irina and Gordon, for talking to me and listening to me when I needed it the most. To Ray and Vicki, for believing in me all the way and encouraging me to write tutorials and this book." — Cosmin Pupăză raywenderlich.com 4 Swift Apprentice About the authors Janie Clayton is an independent iOS developer. She spent a year apprenticed to a super genius programming robots and learning the forgotten ways of long term software maintenance and development. Janie is the coauthor on several books on iOS and Swift development. Janie lives outside of Madison, Wisconsin with her attempted grumble of pugs and multitude of programming books. Janie writes her various musings on her blog at RedQueenCoder.com. Alexis Gallagher is a software engineer who is always looking for the conceptual deep dive and always hoping to find pearls down at the bottom. When he’s not coding, he’s out and about in sunny San Francisco. Matt Galloway is a software engineer with a passion for excellence. He stumbled into iOS programming when it first was a thing, and has never looked back. When not coding, he likes to brew his own beer. Eli Ganem is an engineer at Facebook. He is passionate about teaching, writing, and sharing his knowledge with others. Erik Kerber is a software developer in Minneapolis, MN, and the lead iOS developer for the Target app. He does his best to balance a life behind the keyboard with cycling, hiking, scuba diving, and traveling. Ben Morrow delights in discovering the unspoken nature of the world. He’ll tell you the surprising bits while on a walk. He produces beauty by drawing out the raw wisdom that exists within each of us. raywenderlich.com 5 Swift Apprentice Cosmin Pupăză is a software developer and tutorial writer from Romania and this is his first book. He is part of the Swift tutorial teams at AppCoda and raywenderlich.com. He has worked with more than a dozen programming languages, technologies, frameworks, tools and libraries over the years, but none of them has made such a great impact on himself as the advent of Swift. When not coding, he either plays the guitar or studies WWII history, and he hopes you enjoy reading the book just as much as he enjoyed writing it. Cosmin blogs about Swift at cosminpupaza.wordpress.com. About the editors Steven Van Impe is the technical editor of this book. Steven is a computer science lecturer at the University College of Ghent, Belgium. When he’s not teaching, Steven can be found on his bike, rattling over cobblestones and sweating up hills, or relaxing around the table, enjoying board games with friends. You can find Steven on Twitter as @svanimpe. Chris Belanger is the editor of this book. Chris Belanger is the Book Team Lead and Lead Editor for raywenderlich.com. If there are words to wrangle or a paragraph to ponder, he’s on the case. When he kicks back, you can usually find Chris with guitar in hand, looking for the nearest beach, or exploring the lakes and rivers in his part of the world in a canoe. Ray Fix is the final pass editor of this book. Ray Fix is a software developer from Southern California who is passionate about technology, communication, and helping people become their best. In addition to his role as Swift Team Lead for raywenderlich.com, he is an organizer for the Orange County iOS developer group and a board member for the non-profit, San Diego iOS group. Ray is semi-fluent in spoken and written Japanese and stays healthy by walking, jogging and playing ultimate frisbee. When he is not doing one of those things, he is writing and dreaming of code in Swift! raywenderlich.com 6 Swift Apprentice Table of Contents: Overview Introduction................................................................................ 16 Section I: Swift Basics .................................................. 22 Chapter 1: Coding Essentials & Playground Basics .. 24 Chapter 2: Expressions, Variables & Constants......... 36 Chapter 3: Types & Operations................................... 51 Chapter 4: Basic Control Flow ...................................... 67 Chapter 5: Advanced Control Flow ............................. 84 Chapter 6: Functions ....................................................... 99 Chapter 7: Optionals................................................... 112 Section II: Collection Types ........................................ 124 Chapter 8: Arrays......................................................... 126 Chapter 9: Dictionaries................................................ 139 Chapter 10: Collection Iteration with Closures........ 148 Section III: Building Your Own Types......................... 159 Chapter 11: Structures................................................. 161 Chapter 12: Properties................................................ 173 Chapter 13: Methods................................................... 185 Chapter 14: Classes..................................................... 198 Chapter 15: Advanced Classes.................................. 213 Chapter 16: Enumerations........................................... 233 Chapter 17: Protocols.................................................. 249 raywenderlich.com 7 Swift Apprentice Chapter 18: Generics .................................................. 266 Section IV: Advanced Topics...................................... 281 Chapter 19: Access Control and Code Organization.................................................................. 282 Chapter 20: Custom Operators and Subscripts...... 296 Chapter 21: Pattern Matching ................................... 305 Chapter 22: Error Handling........................................ 324 Chapter 23: Asynchronous Closures and Memory Management.................................................................. 341 Chapter 24: Value Types and Value Semantics...... 354 Chapter 25: Protocol-Oriented Programming ........ 369 Conclusion ............................................................................... 383 raywenderlich.com 8 Swift Apprentice Table of Contents: Extended Introduction................................................................................ 16 Who this book is for.......................................................................................................... 16 What you need.................................................................................................................. 17 How to use this book......................................................................................................... 17 What's in store................................................................................................................... 19 Book source code and forums......................................................................................... 19 Book updates...................................................................................................................... 20 License.................................................................................................................................. 20 Acknowledgments.............................................................................................................. 20 About the cover.................................................................................................................. 21 Section I: Swift Basics .................................................. 22 Chapter 1: Coding Essentials & Playground Basics .......... 24 How a computer works..................................................................................................... 24 Playgrounds........................................................................................................................ 30 Key points............................................................................................................................ 34 Where to go from here?.................................................................................................. 35 Chapter 2: Expressions, Variables & Constants................. 36 Code comments.................................................................................................................. 36 Printing out.......................................................................................................................... 37 Arithmetic operations........................................................................................................ 38 Math functions..................................................................................................................... 42 Naming data...................................................................................................................... 43 Increment and decrement................................................................................................. 47 Key points............................................................................................................................ 48 Where to go from here?.................................................................................................. 48 Challenges........................................................................................................................... 49 Chapter 3: Types & Operations........................................... 51 Type conversion.................................................................................................................. 51 Strings.................................................................................................................................. 55 Strings in Swift.................................................................................................................... 58 Tuples................................................................................................................................... 60 A whole lot of number types........................................................................................... 62 raywenderlich.com 9 Swift Apprentice A peek behind the curtains: Protocols............................................................................ 63 Key points............................................................................................................................ 64 Where to go from here?.................................................................................................. 65 Challenges........................................................................................................................... 65 Chapter 4: Basic Control Flow............................................... 67 Comparison operators...................................................................................................... 67 The if statement.................................................................................................................. 72 Loops.................................................................................................................................... 77 Key Points............................................................................................................................ 80 Where to go from here?.................................................................................................. 81 Challenges........................................................................................................................... 81 Chapter 5: Advanced Control Flow...................................... 84 Ranges.................................................................................................................................. 84 For loops.............................................................................................................................. 85 Switch statements............................................................................................................... 91 Key points............................................................................................................................ 96 Where to go from here?.................................................................................................. 97 Challenges........................................................................................................................... 97 Chapter 6: Functions................................................................ 99 Function basics.................................................................................................................... 99 Functions as variables..................................................................................................... 106 Key points......................................................................................................................... 109 Where to go from here?................................................................................................ 109 Challenges........................................................................................................................ 110 Chapter 7: Optionals ........................................................... 112 Introducing nil................................................................................................................... 112 Introducing optionals....................................................................................................... 114 Unwrapping optionals.................................................................................................... 116 Introducing guard............................................................................................................ 119 Nil coalescing................................................................................................................... 121 Key points......................................................................................................................... 121 Where to go from here?................................................................................................ 122 Challenges........................................................................................................................ 122 raywenderlich.com 10

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.