ebook img

Swift 3 for Absolute Beginners PDF

334 Pages·2016·17.68 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 3 for Absolute Beginners

Swift 3 for Absolute Beginners Third Edition — Gary Bennett Brad Lees NARS WEBI NG NI TRAI Swift 3 for Absolute Beginners Third Edition Gary Bennett Brad Lees Swift 3 for Absolute Beginners Gary Bennett Brad Lees Scottsdale, Arizona, USA Phoenix, Arizona, USA ISBN-13 (pbk): 978-1-4842-2330-7 ISBN-13 (electronic): 978-1-4842-2331-4 DOI 10.1007/978-1-4842-2331-4 Library of Congress Control Number: 2016962063 Copyright © 2016 by Gary Bennett and Brad Lees This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director: Welmoed Spahr Lead Editor: Aaron Black Technical Reviewer: Stefan Kaczmarek Editorial Board: Steve Anglin, Pramila Balen, Louise Corrigan, James DeWolf, Jonathan Gennick, Robert Hutchinson, Celestin Suresh John, Nikhil Karkal, Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing Coordinating Editor: Jessica Vakili Copy Editor: Ann Dickson Compositor: SPi Global Indexer: SPi Global Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springer.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales. Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/. Printed on acid-free paper Contents at a Glance About the Authors ��������������������������������������������������������������������������������������������������xiii About the Technical Reviewer ���������������������������������������������������������������������������������xv Introduction �����������������������������������������������������������������������������������������������������������xvii ■ Chapter 1: Becoming a Great iOS Developer ���������������������������������������������������������1 ■ Chapter 2: Programming Basics ��������������������������������������������������������������������������11 ■ Chapter 3: It’s All About the Data ������������������������������������������������������������������������23 ■ Chapter 4: Making Decisions, Program Flow, and App Design ����������������������������37 ■ Chapter 5: Object-Oriented Programming with Swift ������������������������������������������59 ■ Chapter 6: Learning Swift and Xcode ������������������������������������������������������������������77 ■ Chapter 7: Swift Classes, Objects, and Methods �������������������������������������������������97 ■ Chapter 8: Programming Basics in Swift ����������������������������������������������������������125 ■ Chapter 9: Comparing Data �������������������������������������������������������������������������������151 ■ Chapter 10: Creating User Interfaces ����������������������������������������������������������������165 ■ Chapter 11: Storing Information ������������������������������������������������������������������������193 ■ Chapter 12: Protocols and Delegates ����������������������������������������������������������������219 ■ Chapter 13: Introducing the Xcode Debugger ���������������������������������������������������237 ■ Chapter 14: A Swift iPhone App ������������������������������������������������������������������������251 ■ Chapter 15: Apple Watch and WatchKit �������������������������������������������������������������271 ■ Chapter 16: A Swift HealthKit iPhone App ���������������������������������������������������������297 Index ���������������������������������������������������������������������������������������������������������������������317 iii Contents About the Authors ��������������������������������������������������������������������������������������������������xiii About the Technical Reviewer ���������������������������������������������������������������������������������xv Introduction �����������������������������������������������������������������������������������������������������������xvii ■ Chapter 1: Becoming a Great iOS Developer ���������������������������������������������������������1 Thinking Like a Developer ������������������������������������������������������������������������������������������������1 Completing the Development Cycle ���������������������������������������������������������������������������������4 Introducing Object-Oriented Programming ����������������������������������������������������������������������6 Working with the Playground Interface ����������������������������������������������������������������������������8 Summary ��������������������������������������������������������������������������������������������������������������������������9 What’s Next ��������������������������������������������������������������������������������������������������������������������10 Exercises ������������������������������������������������������������������������������������������������������������������������10 ■ Chapter 2: Programming Basics ��������������������������������������������������������������������������11 Touring Xcode �����������������������������������������������������������������������������������������������������������������11 Exploring the Workspace Window ��������������������������������������������������������������������������������������������������������12 Navigating Your Workspace ������������������������������������������������������������������������������������������������������������������13 Editing Your Project Files ����������������������������������������������������������������������������������������������������������������������14 Creating Your First Swift Playground Program ���������������������������������������������������������������15 Installing and Launching Xcode 8 ���������������������������������������������������������������������������������������������������������16 Using Xcode 8 ���������������������������������������������������������������������������������������������������������������������������������������19 Xcode Playground IDE: Editor and Results Areas �����������������������������������������������������������20 Summary ������������������������������������������������������������������������������������������������������������������������22 v ■ Contents ■ Chapter 3: It’s All About the Data ������������������������������������������������������������������������23 Numbering Systems Used in Programming �������������������������������������������������������������������23 Bits �������������������������������������������������������������������������������������������������������������������������������������������������������23 Bytes �����������������������������������������������������������������������������������������������������������������������������������������������������25 Hexadecimal �����������������������������������������������������������������������������������������������������������������������������������������26 Unicode �������������������������������������������������������������������������������������������������������������������������������������������������28 Data Types ����������������������������������������������������������������������������������������������������������������������28 Declaring Constants and Variables ���������������������������������������������������������������������������������29 Optionals ������������������������������������������������������������������������������������������������������������������������30 Using Variables in Playgrounds ��������������������������������������������������������������������������������������31 Summary ������������������������������������������������������������������������������������������������������������������������35 ■ Chapter 4: Making Decisions, Program Flow, and App Design ����������������������������37 Boolean Logic �����������������������������������������������������������������������������������������������������������������37 Truth Tables ������������������������������������������������������������������������������������������������������������������������������������������38 Comparison Operators ��������������������������������������������������������������������������������������������������������������������������40 Designing Apps ���������������������������������������������������������������������������������������������������������������40 Pseudocode ������������������������������������������������������������������������������������������������������������������������������������������41 Optionals and Forced Unwrapping �������������������������������������������������������������������������������������������������������43 Flowcharting �����������������������������������������������������������������������������������������������������������������������������������������45 Designing and Flowcharting an Example App ��������������������������������������������������������������������������������������45 The App’s Design ����������������������������������������������������������������������������������������������������������������������������������46 Using Loops to Repeat Program Statements ����������������������������������������������������������������������������������������47 Coding the Example App in Swift �����������������������������������������������������������������������������������49 Nested if Statements and else if Statements ���������������������������������������������������������������������������������������52 Removing Extra Characters ������������������������������������������������������������������������������������������������������������������52 Improving the Code Through Refactoring ���������������������������������������������������������������������������������������������52 Running the App �����������������������������������������������������������������������������������������������������������������������������������52 Design Requirements ���������������������������������������������������������������������������������������������������������������������������53 Summary ������������������������������������������������������������������������������������������������������������������������56 vi ■ Contents ■ Chapter 5: Object-Oriented Programming with Swift ������������������������������������������59 The Object ����������������������������������������������������������������������������������������������������������������������59 What Is a Class? �������������������������������������������������������������������������������������������������������������60 Planning Classes ������������������������������������������������������������������������������������������������������������61 Planning Properties ������������������������������������������������������������������������������������������������������������������������������61 Planning Methods ���������������������������������������������������������������������������������������������������������������������������������63 Implementing the Classes ��������������������������������������������������������������������������������������������������������������������66 Inheritance ���������������������������������������������������������������������������������������������������������������������72 Why Use OOP? ����������������������������������������������������������������������������������������������������������������73 OOP Is Everywhere �������������������������������������������������������������������������������������������������������������������������������73 Eliminate Redundant Code �������������������������������������������������������������������������������������������������������������������73 Ease of Debugging ��������������������������������������������������������������������������������������������������������������������������������74 Ease of Replacement ����������������������������������������������������������������������������������������������������������������������������74 Advanced Topics �������������������������������������������������������������������������������������������������������������74 Interface �����������������������������������������������������������������������������������������������������������������������������������������������74 Polymorphism ���������������������������������������������������������������������������������������������������������������������������������������74 Summary ������������������������������������������������������������������������������������������������������������������������75 ■ Chapter 6: Learning Swift and Xcode ������������������������������������������������������������������77 A Newcomer �������������������������������������������������������������������������������������������������������������������77 Understanding the Language Symbols ���������������������������������������������������������������������������77 Implementing Objects in Swift ���������������������������������������������������������������������������������������78 Writing Another Program in Xcode ���������������������������������������������������������������������������������80 Creating the Project ������������������������������������������������������������������������������������������������������������������������������81 Summary ������������������������������������������������������������������������������������������������������������������������96 ■ Chapter 7: Swift Classes, Objects, and Methods �������������������������������������������������97 Creating a Swift Class ����������������������������������������������������������������������������������������������������97 Instance Variables ��������������������������������������������������������������������������������������������������������������������������������98 Methods ������������������������������������������������������������������������������������������������������������������������������������������������99 vii ■ Contents Using Your New Class ���������������������������������������������������������������������������������������������������100 Creating Your Project ��������������������������������������������������������������������������������������������������������������������������100 Adding Objects �����������������������������������������������������������������������������������������������������������������������������������103 Writing the Class ��������������������������������������������������������������������������������������������������������������������������������107 Creating the User Interface �����������������������������������������������������������������������������������������������������������������110 Hooking Up the Code ��������������������������������������������������������������������������������������������������������������������������115 Running the Program ��������������������������������������������������������������������������������������������������������������������������119 Taking Type methods to the Next Level ����������������������������������������������������������������������������������������������121 Accessing the Xcode Documentation ���������������������������������������������������������������������������121 Summary ����������������������������������������������������������������������������������������������������������������������122 ■ Chapter 8: Programming Basics in Swift ����������������������������������������������������������125 Using let vs� var ������������������������������������������������������������������������������������������������������������125 Understanding Collections �������������������������������������������������������������������������������������������126 Using Arrays �����������������������������������������������������������������������������������������������������������������126 Using the Dictionary Class �������������������������������������������������������������������������������������������128 Creating the BookStore Application ������������������������������������������������������������������������������129 Creating Your Class �����������������������������������������������������������������������������������������������������������������������������133 Introducing Properties ������������������������������������������������������������������������������������������������������������������������134 Accessing Properties ��������������������������������������������������������������������������������������������������������������������������135 Finishing the BookStore Program���������������������������������������������������������������������������������135 Creating the View �������������������������������������������������������������������������������������������������������������������������������135 Adding Properties �������������������������������������������������������������������������������������������������������������������������������138 Adding a Description ��������������������������������������������������������������������������������������������������������������������������140 Creating a Simple Data Model Class ��������������������������������������������������������������������������������������������������142 Modifying MasterViewController ��������������������������������������������������������������������������������������������������������144 Modifying the DetailViewController ����������������������������������������������������������������������������������������������������147 Summary ����������������������������������������������������������������������������������������������������������������������149 viii ■ Contents ■ Chapter 9: Comparing Data �������������������������������������������������������������������������������151 Revisiting Boolean Logic ����������������������������������������������������������������������������������������������151 Using Relational Operators �������������������������������������������������������������������������������������������152 Comparing Numbers ���������������������������������������������������������������������������������������������������������������������������152 Creating an Example Xcode App ���������������������������������������������������������������������������������������������������������153 Using Boolean Expressions ������������������������������������������������������������������������������������������158 Comparing Strings ������������������������������������������������������������������������������������������������������������������������������159 Using the switch Statement �����������������������������������������������������������������������������������������160 Comparing Dates ��������������������������������������������������������������������������������������������������������������������������������161 Combining Comparisons ���������������������������������������������������������������������������������������������������������������������162 Summary ����������������������������������������������������������������������������������������������������������������������163 ■ Chapter 10: Creating User Interfaces ����������������������������������������������������������������165 Understanding Interface Builder ����������������������������������������������������������������������������������166 The Model-View-Controller Pattern ������������������������������������������������������������������������������166 Human Interface Guidelines �����������������������������������������������������������������������������������������168 Creating an Example iPhone App with Interface Builder ���������������������������������������������169 Using Interface Builder �����������������������������������������������������������������������������������������������������������������������175 The Document Outline ������������������������������������������������������������������������������������������������������������������������176 The Object Library ������������������������������������������������������������������������������������������������������������������������������177 Inspector Pane and Selector Bar ��������������������������������������������������������������������������������������������������������180 Creating the View �������������������������������������������������������������������������������������������������������������������������������181 Using Outlets ��������������������������������������������������������������������������������������������������������������������������������������182 Using Actions ��������������������������������������������������������������������������������������������������������������������������������������185 The Class ��������������������������������������������������������������������������������������������������������������������������������������������187 Summary ����������������������������������������������������������������������������������������������������������������������190 ■ Chapter 11: Storing Information ������������������������������������������������������������������������193 Storage Considerations ������������������������������������������������������������������������������������������������193 Preferences ������������������������������������������������������������������������������������������������������������������193 Writing Preferences ����������������������������������������������������������������������������������������������������������������������������194 Reading Preferences ��������������������������������������������������������������������������������������������������������������������������195 ix ■ Contents Databases ��������������������������������������������������������������������������������������������������������������������195 Storing Information in a Database��������������������������������������������������������������������������������195 Getting Started with Core Data �������������������������������������������������������������������������������������196 The Model ���������������������������������������������������������������������������������������������������������������������198 Managed Object Context ��������������������������������������������������������������������������������������������������������������������207 Setting Up the Interface ����������������������������������������������������������������������������������������������������������������������207 Summary ����������������������������������������������������������������������������������������������������������������������218 ■ Chapter 12: Protocols and Delegates ����������������������������������������������������������������219 Multiple Inheritance �����������������������������������������������������������������������������������������������������219 Understanding Protocols ����������������������������������������������������������������������������������������������221 Protocol Syntax ������������������������������������������������������������������������������������������������������������221 Delegation ��������������������������������������������������������������������������������������������������������������������222 Protocol and Delegation Example ��������������������������������������������������������������������������������222 Getting Started �������������������������������������������������������������������������������������������������������������224 How It Works ����������������������������������������������������������������������������������������������������������������236 Summary ����������������������������������������������������������������������������������������������������������������������236 ■ Chapter 13: Introducing the Xcode Debugger ���������������������������������������������������237 Getting Started with Debugging �����������������������������������������������������������������������������������237 Setting Breakpoints ����������������������������������������������������������������������������������������������������������������������������238 Using the Breakpoint Navigator ����������������������������������������������������������������������������������������������������������239 Debugging Basics �������������������������������������������������������������������������������������������������������������������������������242 Working with the Debugger Controls ��������������������������������������������������������������������������������������������������244 Using the Step Controls ������������������������������������������������������������������������������������������������245 Looking at the Thread Window and Call Stack �����������������������������������������������������������������������������������246 Debugging Variables ���������������������������������������������������������������������������������������������������������������������������246 Dealing with Code Errors and Warnings �����������������������������������������������������������������������248 Errors ��������������������������������������������������������������������������������������������������������������������������������������������������248 Warnings���������������������������������������������������������������������������������������������������������������������������������������������249 Summary ����������������������������������������������������������������������������������������������������������������������250 x

Description:
This book is perfect for those with no programming background, those with some programming experience but no object-oriented experience, or those that have a great idea for an app but haven’t programmed since school, and it is now updated for Swift 3.Gary Bennett and Brad Lees are full-time profes
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.