ebook img

Swift 2 for Absolute Beginners, 2nd Edition PDF

345 Pages·2015·14.66 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 2 for Absolute Beginners, 2nd Edition

A p BOOKS FOR PROFESSIONALS BY PROFESSIONALS® ple In iPhone, iPad and Apple Watch Made Easy Wat clu ch de an s d H e ES S alt L h D TIT Swift 2 for Absolute Beginners is perfect for those with no w Kit E AT programming background, those with some programming L RE experience but no object-oriented experience, or those that i have a great idea for an app but haven’t programmed since f school. t Gary Bennett and Brad Lees are full-time professional iOS 2 developers and have developed a broad spectrum of apps for Fortune 500 companies. The authors have taken their combined f 14 years of writing apps, the experience from their first three o iOS books, along with their online instruction and free online r forum at XcelMe.com to create an excellent training book. A Many people have a difficult time believing they can learn b to write iOS apps or just staying motivated through learning s o the process. This book, along with the free, live online training l sessions, helps students stay motivated and overcome obstacles u while they learn to be great iOS developers. Here are some of t the highlights: e B • Use Swift Playgrounds to learn iOS development quickly e • Learn about Swift classes, properties, and functions g • How to write iPhone and Apple Watch apps from scratch i Swift 2 n • Introduction to HealthKit and Internet of Things n e r s for Absolute Beginners SECOND EDITION B e n Ln SECOND EDITION ee et st Gary Bennett Brad Lees ISBN 978-1-4842-1489-3 US $ 49.99 54999 Shelve in: S Mobile Computing A R N SOURCE CODE ONLINE BI User level: W E www.apress.com Beginning 9781484214893 N G NI AI www.it-ebooks.info TR Swift 2 for Absolute Beginners Gary Bennett Brad Lees www.it-ebooks.info Swift 2 for Absolute Beginners Copyright © 2015 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. ISBN-13 (pbk): 978-1-4842-1489-3 ISBN-13 (electronic): 978-1-4842-1488-6 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: Michelle Lowman Development Editor: Douglas Pundick Technical Reviewer: Stefan Kaczmarek Editorial Board: Steve Anglin, Louise Corrigan, James DeWolf, Jonathan Gennick, Robert Hutchinson, Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing, Steve Weiss Coordinating Editors: Kevin Walter and Mark Powers Copy Editor: Kezia Endsley Compositor: SPi Global Indexer: SPi Global Artist: 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.springeronline.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 material referenced by the author in this text is available to r eaders at www.apress.com/9781484214893 or http://forum.xcelme.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/. Readers can also access source code at SpringerLink in the Supplementary Material section for each chapter. www.it-ebooks.info Gary would like to dedicate this book to wife Stefanie and children, Michael, Danielle, Michelle, and Emily, for always supporing him. Brad would like to dedicate this book to his wife Natalie, for always supporting him. He couldn’t do it without her. www.it-ebooks.info Contents at a Glance About the Authors ����������������������������������������������������������������������������������������������������xv About the Technical Reviewer �������������������������������������������������������������������������������xvii Acknowledgments ��������������������������������������������������������������������������������������������������xix Introduction ������������������������������������������������������������������������������������������������������������xxi ■ Chapter 1: Becoming a Great iOS Developer ���������������������������������������������������������1 ■ Chapter 2: Programming Basics ��������������������������������������������������������������������������11 ■ Chapter 3: It’s All About the Data ������������������������������������������������������������������������25 ■ Chapter 4: Making Decisions, Program Flow, and App Design ����������������������������41 ■ Chapter 5: Object-Oriented Programming with Swift ������������������������������������������65 ■ Chapter 6: Learning Swift and Xcode ������������������������������������������������������������������83 ■ Chapter 7: Swift Classes, Objects, and Methods �����������������������������������������������105 ■ Chapter 8: Programming Basics in Swift ����������������������������������������������������������131 ■ Chapter 9: Comparing Data �������������������������������������������������������������������������������157 ■ Chapter 10: Creating User Interfaces ����������������������������������������������������������������173 ■ Chapter 11: Storing Information ������������������������������������������������������������������������199 ■ Chapter 12: Protocols and Delegates ����������������������������������������������������������������231 v www.it-ebooks.info vi Contents at a Glance ■ Chapter 13: Introducing the Xcode Debugger ���������������������������������������������������247 ■ Chapter 14: A Swift iPhone App ������������������������������������������������������������������������263 ■ Chapter 15: Apple Watch and watchKit �������������������������������������������������������������281 ■ Chapter 16: A Swift HealthKit iPhone App ���������������������������������������������������������305 Index ���������������������������������������������������������������������������������������������������������������������325 www.it-ebooks.info Contents About the Authors ����������������������������������������������������������������������������������������������������xv About the Technical Reviewer �������������������������������������������������������������������������������xvii Acknowledgments ��������������������������������������������������������������������������������������������������xix Introduction ������������������������������������������������������������������������������������������������������������xxi ■ Chapter 1: Becoming a Great iOS Developer ���������������������������������������������������������1 Thinking Like a Developer ������������������������������������������������������������������������������������������������2 Completing the Development Cycle ���������������������������������������������������������������������������������5 Introducing Object-Oriented Programming ����������������������������������������������������������������������6 Working with the Playground Interface ����������������������������������������������������������������������������9 Summary ������������������������������������������������������������������������������������������������������������������������10 What’s Next ��������������������������������������������������������������������������������������������������������������������10 Exercises ������������������������������������������������������������������������������������������������������������������������10 ■ Chapter 2: Programming Basics ��������������������������������������������������������������������������11 Touring Xcode �����������������������������������������������������������������������������������������������������������������11 Exploring the Workspace Window ��������������������������������������������������������������������������������������������������������12 Navigating Your Workspace ������������������������������������������������������������������������������������������������������������������14 Editing Your Project Files ����������������������������������������������������������������������������������������������������������������������14 vii www.it-ebooks.info viii Contents Creating Your First Swift Playground Program ���������������������������������������������������������������15 Installing and Launching Xcode 7 ���������������������������������������������������������������������������������������������������������16 Using Xcode 7 ���������������������������������������������������������������������������������������������������������������������������������������19 Xcode Playground IDE: Editor and Results Areas �����������������������������������������������������������20 Summary ������������������������������������������������������������������������������������������������������������������������22 Exercise ��������������������������������������������������������������������������������������������������������������������������23 ■ Chapter 3: It’s All About the Data ������������������������������������������������������������������������25 Numbering Systems Used in Programming �������������������������������������������������������������������25 Bits �������������������������������������������������������������������������������������������������������������������������������������������������������25 Bytes �����������������������������������������������������������������������������������������������������������������������������������������������������28 Hexadecimal �����������������������������������������������������������������������������������������������������������������������������������������29 Unicode �������������������������������������������������������������������������������������������������������������������������������������������������30 Data Types ����������������������������������������������������������������������������������������������������������������������31 Declaring Constants and Variables ���������������������������������������������������������������������������������32 Optionals ������������������������������������������������������������������������������������������������������������������������33 Using Variables in Playgrounds ��������������������������������������������������������������������������������������34 Summary ������������������������������������������������������������������������������������������������������������������������39 Exercises ������������������������������������������������������������������������������������������������������������������������39 ■ Chapter 4: Making Decisions, Program Flow, and App Design ����������������������������41 Boolean Logic �����������������������������������������������������������������������������������������������������������������41 Truth Tables ������������������������������������������������������������������������������������������������������������������������������������������43 Comparison Operators ��������������������������������������������������������������������������������������������������������������������������44 Designing Apps ���������������������������������������������������������������������������������������������������������������45 Pseudocode ������������������������������������������������������������������������������������������������������������������������������������������45 Optionals and Forced Unwrapping �������������������������������������������������������������������������������������������������������48 Flowcharting �����������������������������������������������������������������������������������������������������������������������������������������50 Designing and Flowcharting an Example App ��������������������������������������������������������������������������������������50 The App’s Design ����������������������������������������������������������������������������������������������������������������������������������51 Using Loops to Repeat Program Statements ����������������������������������������������������������������������������������������52 www.it-ebooks.info Contents ix Coding the Example App in Swift �����������������������������������������������������������������������������������54 Nested if Statements and else if Statements ���������������������������������������������������������������������������������������57 Removing Extra Characters ������������������������������������������������������������������������������������������������������������������57 Improving the Code Through Refactoring ���������������������������������������������������������������������������������������������57 Running the App �����������������������������������������������������������������������������������������������������������������������������������58 Design Requirements ���������������������������������������������������������������������������������������������������������������������������59 Summary ������������������������������������������������������������������������������������������������������������������������62 Exercises ������������������������������������������������������������������������������������������������������������������������63 ■ Chapter 5: Object-Oriented Programming with Swift ������������������������������������������65 The Object ����������������������������������������������������������������������������������������������������������������������65 What Is a Class? �������������������������������������������������������������������������������������������������������������66 Planning Classes ������������������������������������������������������������������������������������������������������������67 Planning Properties ������������������������������������������������������������������������������������������������������������������������������67 Planning Methods ���������������������������������������������������������������������������������������������������������������������������������69 Implementing the Classes ��������������������������������������������������������������������������������������������������������������������71 Inheritance ���������������������������������������������������������������������������������������������������������������������78 Why Use OOP? ����������������������������������������������������������������������������������������������������������������79 OOP Is Everywhere �������������������������������������������������������������������������������������������������������������������������������79 Eliminate Redundant Code �������������������������������������������������������������������������������������������������������������������80 Ease of Debugging ��������������������������������������������������������������������������������������������������������������������������������80 Ease of Replacement ����������������������������������������������������������������������������������������������������������������������������80 Advanced Topics �������������������������������������������������������������������������������������������������������������80 Interface �����������������������������������������������������������������������������������������������������������������������������������������������80 Polymorphism ���������������������������������������������������������������������������������������������������������������������������������������81 Summary ������������������������������������������������������������������������������������������������������������������������81 Exercises ������������������������������������������������������������������������������������������������������������������������82 www.it-ebooks.info x Contents ■ Chapter 6: Learning Swift and Xcode ������������������������������������������������������������������83 A Newcomer �������������������������������������������������������������������������������������������������������������������83 Understanding the Language Symbols ���������������������������������������������������������������������������84 Implementing Objects in Swift ���������������������������������������������������������������������������������������85 Writing Another Program in Xcode ���������������������������������������������������������������������������������87 Creating the Project ������������������������������������������������������������������������������������������������������������������������������88 Summary ����������������������������������������������������������������������������������������������������������������������104 Exercises ����������������������������������������������������������������������������������������������������������������������104 ■ Chapter 7: Swift Classes, Objects, and Methods �����������������������������������������������105 Creating a Swift Class ��������������������������������������������������������������������������������������������������105 Instance Variables ������������������������������������������������������������������������������������������������������������������������������106 Methods ����������������������������������������������������������������������������������������������������������������������������������������������107 Using Your New Class ���������������������������������������������������������������������������������������������������109 Creating Your Project ��������������������������������������������������������������������������������������������������������������������������109 Adding Objects �����������������������������������������������������������������������������������������������������������������������������������112 Writing the Class ��������������������������������������������������������������������������������������������������������������������������������115 Creating the User Interface �����������������������������������������������������������������������������������������������������������������116 Hooking Up the Code ��������������������������������������������������������������������������������������������������������������������������122 Running the Program ��������������������������������������������������������������������������������������������������������������������������126 Taking Class Methods to the Next Level ���������������������������������������������������������������������������������������������127 Accessing the Xcode Documentation ���������������������������������������������������������������������������127 Summary ����������������������������������������������������������������������������������������������������������������������129 Exercises ����������������������������������������������������������������������������������������������������������������������129 www.it-ebooks.info

Description:
Swift 2 for Absolute Beginners 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 2. Gary Bennett and Brad Lees
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.