ebook img

Learning Swift PDF

266 Pages·2015·2.624 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 Learning Swift

Learning Swift Build a solid foundation in Swift to develop smart and robust iOS and OS X applications Andrew J Wagner BIRMINGHAM - MUMBAI Learning Swift Copyright © 2015 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: June 2015 Production reference: 1250615 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78439-250-5 www.packtpub.com Credits Author Project Coordinator Andrew J Wagner Nidhi Joshi Reviewers Proofreader Giordano Scalzo Safis Editing Cory Bohon Indexer Mariammal Chettiyar Commissioning Editor Sarah Crofton Graphics Disha Haria Acquisition Editors Vivek Anantharaman Richard Brookes-Bland Production Coordinator Nilesh R. Mohite Content Development Editor Kirti Patil Cover Work Nilesh R. Mohite Technical Editor Mrunmayee Patil Copy Editors Pranjali Chury Neha Vyas About the Author Andrew J Wagner is an independent software developer who concentrates on iOS development and backend web services and has a degree in computer engineering from Rensselaer Polytechnic Institute, New York. He has also been developing his own iOS apps through his company, Learn Brigade LLC, since 2008 and has experience working with and for large-scale companies and small-scale companies. He is passionate about using computers as a creative outlet and writing software that is beautiful in implementation, functionality, and experience. About the Reviewer Giordano Scalzo has been a developer since the days of the ZX Spectrum. He worked with C++, Java, .NET, Ruby, Python, and a ton of other languages he has forgotten the names of. After years of backend development and a hiatus from the technical side when he worked as a project manager, he is currently a freelancer in London where, through his company, Effective Code Ltd, he delivers code for iOS aiming at quality and reliability. In his spare time, when he is not crafting retro game clones for iOS, he writes his thoughts on his blog at http://giordanoscalzo.com. I'd like to thank my better half, Valentina, who lovingly supports me in everything I do—without you, none of this would have been possible. I would also like to thank my children, Mattia and Luca, who are my future and have given me lots of smiles and hugs when I needed them. www.PacktPub.com Support files, eBooks, discount offers, and more For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub. com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books. Why subscribe? • Fully searchable across every book published by Packt • Copy and paste, print, and bookmark content • On demand and accessible via a web browser Free access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access. Table of Contents Preface vii Chapter 1: Introducing Swift 1 Defining our goals for this book 1 Setting up the development environment 2 Running our first Swift code 3 Understanding playgrounds 5 Learning with this book 6 Summary 6 Chapter 2: Building Blocks – Variables, Collections, and Flow Control 7 Core Swift types 8 Constants and variables 8 Containers 8 Tuples 9 Arrays 9 Dictionaries 10 Swift's type system 11 Printing on the console 13 Control flow 14 Conditionals 14 Switches 15 Loops 18 Functions 21 Basic functions 21 Parameterized functions 22 Functions that return values 23 Functions with default arguments 25 Bringing it all together 26 Summary 31 [ i ] Table of Contents Chapter 3: One Piece at a Time – Types, Scopes, and Projects 33 Structs 34 Types versus instances 34 Properties 35 Member and static methods 36 Computed properties 40 Reacting to property changes 41 Subscripts 42 Custom initialization 43 Classes 46 Inheriting from another class 46 Initialization 48 Overriding initializers 49 Required initializers 49 Designated and convenience initializers 50 Overriding methods and computed properties 51 Methods 52 Computed properties 53 Casting 53 Upcasting 54 Downcasting 55 Enumerations 56 Basic declaration 56 Testing enumeration values 56 Raw values 57 Associated values 58 Methods and properties 60 Projects 61 Setting up a command-line Xcode project 62 Creating and using an external file 64 Interfacing with code from other files 65 File organization and navigation 68 Extensions 69 Scope 70 How is scope defined 70 Nested types 70 Access control 71 Summary 72 Chapter 4: To Be or Not to Be – Optionals 73 Introducing optionals 74 Unwrapping an optional 75 Optional binding 75 [ ii ] Table of Contents Forced unwrapping 77 Nil coalescing 78 Optional chaining 78 Implicitly unwrapped optionals 80 Debugging optionals 82 The underlying implementation 85 Summary 86 Chapter 5: A Modern Paradigm – Closures and Functional Programming 87 Functional programming philosophy 88 State and side effects 88 Declarative versus imperative code 89 Closures 90 Closures as variables 90 Closures as parameters 91 Syntactic sugar 92 Building blocks of functional programming in Swift 94 The filter function 94 The reduce function 94 The map function 96 The sorted function 96 How these affect the state and nature of code 97 Lazy evaluation 97 Curried functions 98 Summary 100 Chapter 6: Make Swift Work for You – Protocols and Generics 101 Protocols 101 Defining a protocol 102 Implementing a protocol 102 Using type aliases 104 Defining a generic 106 The generic function 106 The generic type 108 Type constraints 109 Protocol constraints 109 Where clauses for protocols 111 Where clauses for equality 112 Extending the existing generics 112 Enhancing arrays 113 Enhancing dictionaries 114 [ iii ]

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.