professional swift™ · professional swift™ ... part ii: advanced swift concepts chaer 7: pt...

30

Upload: others

Post on 29-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163
Page 2: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163
Page 3: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

Professional swift™

introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii

▸ Part i Building aPPlications with swift

chaPter 1 A Swift Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

chaPter 2 Writing a Swift Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

chaPter 3 Classes, Structs, and Enums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

chaPter 4 Concurrency in Swift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

chaPter 5 Interfacing with Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

chaPter 6 Storing Data with Core Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

▸ Part ii advanced swift concePts

chaPter 7 Extending Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

chaPter 8 Advanced Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

chaPter 9 Bridging Swift and Objective‐C . . . . . . . . . . . . . . . . . . . . . . . . . . . 189

chaPter 10 Debugging Swift Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207

chaPter 11 The Swift Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229

aPPendix An Overview of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247

index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265

Page 4: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163
Page 5: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

PROfESSIOnAl

swift™

Page 6: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163
Page 7: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

PROfESSIOnAl

swift™

Michael Dippery

Page 8: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

Professional Swift™

Published by John Wiley & Sons, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com

Copyright © 2015 by John Wiley & Sons, Inc., Indianapolis, Indiana

Published simultaneously in Canada

ISBN: 978-1-119-01677-9

ISBN: 978-1-119-14872-2 (ebk)

ISBN: 978-1-119-14871-5 (ebk)

Manufactured in the United States of America

10 9 8 7 6 5 4 3 2 1

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.

Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or pro-motional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the pub-lisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read.

For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.

Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.

Library of Congress Control Number: 2015937849

Trademarks: Wiley, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trademarks or regis-tered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. Swift is a trademark of Apple, Inc. All other trademarks are the property of their respec-tive owners. John Wiley & Sons, Inc., is not associated with any product or vendor mentioned in this book.

Page 9: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

To Jean Gold, for encouraging me to put pen to paper

in the first place.

Page 10: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163
Page 11: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

aBout the author

Michael diPPery is a software developer for Industrial Light & Magic in San Francisco, CA. A graduate of Bucknell University’s computer science program, he has also worked for SocialCode in San Francisco and The New York Review of Books in New York. While most of his day is spent writing Python code, he has long been fond of Objective‐C. He has contributed to both the Adium and Colloquy projects on OS X and has written and released numerous open source libraries for Objective‐C. When he’s not working in Objective‐C, Michael is just crazy enough to enjoy writing code in Haskell and Erlang. He looks forward to the potential for Swift to improve software devel-opment on iOS and OS X. Outside of programming, Michael enjoys reading, writing fiction, and taking photographs with one of his many cameras.

Page 12: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163
Page 13: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

acquisitions editorAaron Black

Project editorMaureen Tullis

technical editorChaim Krause

Production editorDassi Zeidel

coPy editorNancy Rapoport

Manager of content develoPMent & asseMBlyMary Beth Wakefield

Marketing directorDavid Mayhew

Marketing ManagerCarrie Sherrill

Professional technology & strategy directorBarry Pruett

Business ManagerAmy Knies

associate PuBlisherJim Minatel

Project coordinator, coverBrent Savage

ProofreaderRebecca Rider

indexerJohnna VanHoose Dinse

cover designerWiley

cover iMage©iStock.com/OJO_Images

credits

Page 14: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163
Page 15: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

acknowledgMents

thanks to My Parents, steven and kiM, for never wavering in their support of my education and career, even though they’ve never fully understood what it is I do.

Liza Veloz, for giving me my first opportunities as both a technical writer and a programmer.

Professor Luiz Felipe Perrone, for taking me under his wing and providing valuable mentoring and friendship over the last ten years.

Mary James, for inviting me to write a book in the first place.

Page 16: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163
Page 17: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

Contents

IntroduCtIon xxiii

Part I: BuIldIng aPPlICatIons wIth swIft

ChaPter 1: a swIft PrImer 3

What Is Swift? 4Why Learn Swift? 5Working with Constants and Variables 5

Understanding Swift Data Types 6Using Numerical Types 6Boolean Types 7Using Character Types 8Using Arrays 10Using Dictionaries 10Using Tuples 11

Working with Type Annotations 12Minimizing Annotation with Type Inference 13Clarifying Code with Type Aliasing 14

Working with Operators 15Using Basic Operators 15Using Compound Assignment Operators 17Using Overflow Operators 17Using Range Operators 18Using Logical Operators 18Using Comparison Operators 19Using Custom Operators 19

Making Decisions with Control Flow 19Using Conditional Statements 20Using Loops 23Control Transfer Statements 25

Grouping Types with Enumerations 26Working with Functions 27

Declaring Functions 27Specifying Parameter Names 28Defining Default Parameters 29

Page 18: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

xvi

COnTEnTS

Specifying Variadic Parameters 29Specifying Constant, Variable, and In-Out Parameters 30Function Types 31Using Closures 32

Summary 34

ChaPter 2: wrItIng a swIft Program 35

Setting Up Xcode 36Downloading from Apple’s Developer Portal 36Downloading Xcode 36

Experimenting with Playgrounds 38Using Playgrounds in Xcode 38A Simple Example in Playground 38A More Exciting Playground Example 40

Writing Swift in Xcode 41Creating a New Swift Project 42Creating the Swiftisms App 44

Creating the Interface 45Creating Connections between the View Controller and the Object 46Connecting the Buttons and the Action 50Displaying Random Strings 51What Next? 53

Debugging Swift Applications 53Understanding Breakpoint Types 54Setting Breakpoints 54Using the Debugger 55

Using the Hierarchical View 56Using the Debugger View and the po Command 56Executing the Program 58

Summary 59

ChaPter 3: Classes, struCts, and enums 61

Working with Classes and Structs 62Defining Classes and Structs 62Initializing Classes and Structs 64

Creating an Initial Value 64Defining Your Own Initializer 65Writing an Initializer 66Working with Multiple Initializers 68

Deinitializing Classes 69

Page 19: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

xvii

COnTEnTS

Working with Properties 70Using Simple Properties 70Using Lazy Properties 72Using Computed Properties 72Observing Property Changes 73

Understanding Methods 74Understanding the Difference between Classes and Structs 76Working with Enumerations 78Summary 79

ChaPter 4: ConCurrenCy In swIft 81

What Is Concurrency? 82Understanding Processes 82Understanding Threads 84Why Concurrency? 85Designing Concurrent Programs 85

Concurrency in iOS and OS X 86Understanding Event Loops 86Understanding Run Loops 87Understanding Asynchronous Methods 87Understanding NSTimer 89Understanding NSThread 91Understanding Grand Central Dispatch 92

Summary 94

ChaPter 5: InterfaCIng wIth weB servICes 95

Understanding Web Services 95Implementing a Web Services Client 97

Signing Up for a Firebase Account 97Laying Out the User Interface 98Hooking Up the Add Album View Controller 101

Adding the Album Creation View Controller 103Hooking Up the Segue 104

Creating the Album Model 104Communicating with Firebase 106

Creating the Firebase Connector 106Making Asynchronous Web Requests 109Processing JSON Data 110Creating New Albums 111Deleting Albums 112

Wiring Up the View and Data Models 112

Page 20: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

xviii

COnTEnTS

Wiring Up the Segue 115Run the Application 116

Summary 118

ChaPter 6: storIng data wIth Core data 119

What Is Core Data? 120The Core Data Stack 120Using Core Data with Swift 121

Setting Up the Birthdays Application 122Getting User Input 128

Examining Xcode Files 129Adding Methods to the AppDelegate Class 134Handling the Dialog Sheet 135Hooking Up the Dialog Sheet 137

Displaying Data 137Implementing the Table View’s Behavior 138The Completed Application Delegate 140

Summary 145

Part II: advanCed swIft ConCePts

ChaPter 7: extendIng Classes 149

Working with Class Extensions 150Adding Methods to Classes 150Adding Properties to Classes 152Mutating Classes in Extensions 153

Specifying Behavior with Protocols 153Adopting Protocols 154Declaring Properties and Methods 154Working with Optional Methods and Properties 156Protocols Are Types 158

Checking for Protocol Conformance 158Using Protocol Types in Collections 159Composing Protocols 160

Inheriting from Other Protocols 160Adopting Protocols in Class Extensions 161

Working with Generics 163Generic Classes 164Working with Type Constraints 165

Summary 166

Page 21: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

xix

COnTEnTS

ChaPter 8: advanCed data tyPes 167

Working with Enums and Algebraic Data Types 168Matching Patterns 168Putting It All Together with JSON 170

Working with Optional Types 172Working with Null Values 172Excluding Null Values 173Understanding Swift’s Version of Null 173Chaining Optional Values 174

Understanding Type Casting 175Grouping Values with Tuples 177Custom Operators 179

Defining Custom Operators 183Defining Precedence and Associativity 183A Final Word about Operators 186

Using Functions and Closures 187Summary 188

ChaPter 9: BrIdgIng swIft and oBjeCtIve‐C 189

The Successor to Objective-C 190Introducing namespaces and Modules 191

Organizing Code with Namespaces 191Distributing Code with Modules 193

Using Access Modifiers 193Specifying an Access Level 194

How Swift and Objective-C Interact 194Using Swift Classes in Objective-C 195

Generating an Objective-C Header 195Objective-C and Swift-Only Features 196Using Swift Code in an Objective-C Application 196

Using Objective-C in Swift 198Generating a Bridging Header 198Calling Objective-C Code from Swift 199

Using C and C++ Code with Swift 202Working with C Scalar Types 202Accessing Memory with C Pointers 203Working with Global Constants and Defines and Swift 204Calling C Code from Swift 204Using C++ Code in Swift 206

Summary 206

Page 22: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

xx

COnTEnTS

ChaPter 10: deBuggIng swIft aPPlICatIons 207

The Art of Debugging 208Creating CircleView 208

Drawing the Circle 209Adding Sliders 210Completing the Project 211

Printing Values 213Working with Debuggers 215Examining Errors with lldb, the LLVM Debugger 216

Basic Breakpoints 216Using lldb to Inspect Programs 218

Using the Variable Pane 218Entering Commands in the lldb Command Prompt Pane 218Displaying Code Hierarchy with the lldb Ribbon 219Walking through a Program with the lldb Ribbon 221

Removing or Disabling Breakpoints 222Setting Conditional Breakpoints 222Setting Symbolic Breakpoints 223Exploring the Debugger 225

Summary 228

ChaPter 11: the swIft runtIme 229

What Is a Runtime? 230Understanding the Objective‐C Runtime 231

Dispatching Methods Dynamically in Objective‐C 231Message Passing 233Resolving Methods 233Messages and Methods 237

Exploring the Swift Runtime 238Understanding Virtual Method Tables 239Loading Swift Programs 241

Summary 245

aPPendIx: an overvIew of C 247

Comparing Procedural and Object-Oriented Programming Styles 248Understanding the Importance of C Language Syntax 249

Defining Data with Variables and Arrays 249Integral Data Types 249Floating-Point Data Types 251Arrays 252

Page 23: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

xxi

COnTEnTS

Typedefs 253Enums 254

Performing Calculations with Operators 254Arithmetic Operators 254Logical Operators 256Relational Operators 258Bitwise Operators 258Bitshift Operators 259

Referencing Data with Pointers 260Organizing Data with Structs 261Generalizing Data with Unions 261Referencing Functions with Function Pointers 262

Index 265

Page 24: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163
Page 25: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

IntroductIon

the popularIty of IoS has drawn many programmers to Apple’s platforms. Software on both OS X and iOS has long been written in Objective‐C, a language first developed in the 1980s that is best described as an amalgamation of C and Smalltalk. Objective‐C lacks many features that modern programmers expect to find in a programming language. Over the last ten years, Apple has adapted and improved Objective‐C, but many programmers still find it to be an alien language.

Enter Swift. Swift is a modern programming language, built with knowledge gained in the last 30 years of programming language research. Developed by the same researchers who developed clang, the modern compiler toolchain used by Apple to build OS X and iOS software, Swift incorporates many features that are expected of a modern programming language. It sports a strong type system to prevent many of the mistakes prevalent in both C and Objective‐C programming. It has a refined class system complete with static and computable properties. It supports closures and treats func-tions as first‐class objects, allowing them to easily be saved as variables or passed to functions and methods as parameters. As you read this book, you’ll find many more advanced features that are present in Swift as well.

Swift has been designed from the ground up to be a modern programming language suitable for both systems and application development on Apple’s platforms. Apple has already thrown a lot of support behind Swift, and the language is constantly being improved. Swift represents the future of software development on iOS and OS X, and programmers working on that platform should expect Swift to occupy an increasingly dominant position on those systems.

Who thIS Book IS for

This book is aimed at software developers with experience writing Objective‐C applications on OS X or iOS. Some knowledge and experience with Swift are recommended but are by no means necessary to understand the material presented in this book. The book introduces the key concepts of Swift that may be new even to advanced iOS and OS X programmers. Over the course of the book, several small projects are used to illustrate the concepts as they are introduced. The text also makes extensive use of playgrounds, a new feature of Xcode that allows you to write and interact with Swift code without the overhead of an entire Xcode project.

The first chapter is an overview of Swift’s syntax and semantics but does not cover any advanced features of the language. The second chapter covers the use of Xcode, including playgrounds. If you already have some familiarity with Swift and Xcode, you may want to skip the first and second chapters and get started immediately with Chapter 3.

Page 26: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

xxiv

introduction

What thIS Book coverS

This book first offers a primer to the Swift programming language, released by Apple in June 2014. It quickly moves on to more advanced Swift programming topics, including the new playgrounds feature in Xcode; classes, structs, and enums; concurrent programming; and advanced features of Swift’s type system. It also covers practical topics that you will deal with on a regular basis as a Swift programmer, including interfacing with web services using JSON, Core Data, and using C and Objective‐C code in your Swift project. It shows some of the advanced features of Xcode, includ-ing the use of the debugger built in to Xcode, and discusses the lower‐level details of the Swift and Objective‐C runtimes. And because it is impossible to completely avoid C when writing software on OS X and iOS, an index provides a quick overview of the C programming language.

Swift has been constantly evolving since its release in June 2014. This book covers version 1.1.

Because Swift is still changing, you may find some minor differences between the version discussed in this book and the latest released version. However, the vast majority of material presented in this book will still be valid for future versions of Swift.

hoW thIS Book IS Structured

This book is divided into two sections. The first section provides an introduction to the Swift programming language and covers many of the more practical topics you will encounter as a Swift programmer. The second section is a “deep dive” into the intricacies of Swift.

➤ Chapter 1: Reviews the syntax and semantics of the Swift programming language. It is useful for those programmers who are new to the language or want a quick refresher.

➤ Chapter 2: Covers the Swift‐related changes to Xcode. It introduces Xcode’s new feature, playgrounds, which provide a way to interactively work with Swift code.

➤ Chapter 3: Covers Swift’s classes, structs, and enums in greater detail than Chapter 1, providing a solid foundation for object‐oriented programming in Swift.

➤ Chapter 4: Discusses concurrent programming in the context of Swift and the Foundation framework on iOS and OS X.

➤ Chapter 5: Shows you how to communicate with remote web services using JSON.

➤ Chapter 6: Discusses how to use Core Data to store and search for information used by your program.

➤ Chapter 7: Shows you how to extend classes using protocols and class extensions.

➤ Chapter 8: Covers Swift’s type system in fine detail and shows you how to take advantage of Swift’s strong type system to write less error‐prone code.

➤ Chapter 9: Shows you how you can mix C and Objective‐C code in your Swift programs.

Page 27: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

xxv

introduction

➤ Chapter 10: Introduces the debugger and shows you how you can track down and fix problems in your code.

➤ Chapter 11: Discusses the runtime architecture of both Swift and Objective‐C and demon-strates how programs are loaded and executed on iOS and OS X.

➤ Appendix: Covers the features and concepts that the C programming language introduces.

If you are already familiar with Swift and Xcode, you may want to skip Chapters 1 and 2 and start right in on Chapter 3.

What you need to uSe thIS Book

To run the samples in the book, you will need the following:

➤ A Mac running OS X 10.9 or later

➤ Xcode 6

➤ An iPhone, iPod Touch, or iPad if you want to run the examples on an actual device instead of the iOS simulator that ships with Xcode

The source code for the samples is available for download from the Wrox website at: www.wrox.com/go/proswift.

conventIonS

To help you get the most from the text and keep track of what’s happening, this book uses a number of conventions.

WarnInG Warnings hold important, not‐to‐be‐forgotten information that is directly relevant to the surrounding text.

note Notes indicate notes, tips, hints, tricks, or asides to the current discussion.

As for styles in the text:

➤ We highlight new terms and important words when we introduce them.

➤ We show keyboard strokes like this: Ctrl+A.

Page 28: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

xxvi

introduction

➤ We show file names, URLs, and code within the text like so: persistence.properties.

We present code in two different ways:

We use a monofont type with no highlighting for most code examples.

We use bold to emphasize code that is particularly important in the present context or to show changes from a previous code snippet.

Source code

As you work through the examples in this book, you may choose either to type in all the code man-ually or to use the source code files that accompany the book. All the source code used in this book is available for download at www.wrox.com. Specifically for this book, the code download is on the Download Code tab at www.wrox.com/go/proswift.

You can also search for the book at www.wrox.com by ISBN (the ISBN for this book is 978‐1‐119‐01677‐9) to find the code. And a complete list of code downloads for all current Wrox books is available at www.wrox.com/dynamic/books/download.aspx.

At the beginning of each chapter, we’ve provided a list of the major code files for the chapter. Throughout each chapter, you’ll also find references to the names of code files as needed in listing titles and text.

Most of the code on www.wrox.com is compressed in a .ZIP, .RAR, or similar archive format appropriate to the platform. Once you download the code, just decompress it with an appropriate compression tool.

note Because many books have similar titles, you may find it easiest to search by ISBN; this book’s ISBN is 978‐1‐119‐01677‐9.

Once you download the code, just decompress it with your favorite compression tool. Alternately, you can go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.

errata

We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, such as a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata, you may save another reader hours of frustration, and at the same time, you will be helping us provide even higher quality information.

To find the errata page for this book, go to www.wrox.com/go/proswift, and click the Errata link. On this page you can view all errata that has been submitted for this book and posted by Wrox editors.

Page 29: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163

xxvii

introduction

If you don’t spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We’ll check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions of the book.

p2p.Wrox.com

For author and peer discussion, join the P2P forums at http://p2p.wrox.com. The forums are a Web‐based system to which you can post messages relating to Wrox books and related technologies and where you can interact with other readers and technology users. The forums offer a subscription feature to e‐mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.

At http://p2p.wrox.com, you will find a number of different forums that will help you, not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:

1. Go to http://p2p.wrox.com and click the Register link.

2. Read the terms of use and click Agree.

3. Complete the required information to join, as well as any optional information you wish to provide, and click Submit.

4. You will receive an e‐mail with information describing how to verify your account and com-plete the joining process.

note You can read messages in the forums without joining P2P, but in order to post your own messages, you must join.

Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e‐mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works, as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.

Page 30: Professional swift™ · Professional swift™ ... Part II: advanCed swIft ConCePts Chaer 7: Pt extendIng Classes 149 Working with Class Extensions 150 ... Working with Generics 163