Tải bản đầy đủ (.pdf) (529 trang)

OReilly learning swift 3 early release

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (23.58 MB, 529 trang )



Learning Swift 3

Jon Manning, Paris Buttfield-Addison, and Tim Nugent

Beijing

Boston Farnham Sebastopol

Tokyo


Learning Swift 3
by Paris Buttfield-Addison , Jon Manning , and Tim Nugent
Copyright © 2016 Secret Lab. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles ( ). For more information, contact our corporate/
institutional sales department: 800-998-9938 or .

Editor: Rachel Roumeliotis
Production Editor: FILL IN PRODUCTION EDI‐
TOR

Copyeditor: FILL IN COPYEDITOR
Proofreader: FILL IN PROOFREADER
January -4712:

Indexer: FILL IN INDEXER


Interior Designer: David Futato
Cover Designer: Karen Montgomery
Illustrator: Rebecca Demarest

First Edition

Revision History for the First Edition
2016-12-05:

First Early Release

See for release details.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Learning Swift 3, the cover image, and
related trade dress are trademarks of O’Reilly Media, Inc.
While the publisher and the author(s) have used good faith efforts to ensure that the information and
instructions contained in this work are accurate, the publisher and the author(s) disclaim all responsibil‐
ity for errors or omissions, including without limitation responsibility for damages resulting from the use
of or reliance on this work. Use of the information and instructions contained in this work is at your own
risk. If any code samples or other technology this work contains or describes is subject to open source
licenses or the intellectual property rights of others, it is your responsibility to ensure that your use
thereof complies with such licenses and/or rights.

978-1-491-96699-0
[FILL IN]


Table of Contents

Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix


Part I.

Swift Basics

1. Getting Started. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
The Apple Developer Program
Registering for the Apple Developer Program
Downloading Xcode
Creating Your First Project with Xcode
The Xcode Interface
Developing a Simple Swift Application
Designing the Interface
Connecting the Code
Using the iOS Simulator
Conclusion

5
6
7
8
12
20
21
22
24
26

2. The Basics of Swift. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
The Swift Programming Language
Swift 2 Versus Swift 3

Playgrounds
Comments
Variables and Constants
Operators
Control Flow
Loops
Switches
Types

28
30
31
32
33
34
35
36
37
40
iii


Working with Strings
Comparing Strings
Searching Strings
Optional Types
Type Casting
Tuples
Arrays
Dictionaries

Enumerations
Sets
Functions and Closures
Using Functions as Variables
Closures
The defer Keyword
The guard Keyword
Making your code Swifty
Conclusion

40
41
42
42
44
45
46
48
48
50
51
54
56
58
58
59
59

3. Swift for Object-Oriented App Development. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Classes and Objects

Initialization and Deinitialization
Properties
Inheritance
Protocols
Extensions
Access Control
Operator Overloading
Generics
Subscripts
Structures
Modules
The Swift Standard Library, Foundation, Cocoa, and Cocoa Touch
Swift Package Manager
Data
Loading Data from Files and URLs
Serialization and Deserialization
Error Handling
Memory Management
Design Patterns in Cocoa and Cocoa Touch
Model-View-Controller
Delegation
Structuring an App

iv

| Table of Contents

61
63
64

64
68
69
70
72
73
74
75
76
76
77
79
79
80
81
84
85
85
87
89


The Application Delegate
Window Controllers and View Controllers
Nibs and Storyboards
Conclusion

89
89
90

90

Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

Part II. An OS X App
4. Setting Up the OS X Notes App. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Designing the OS X Notes App
Creating the OS X Project
Defining a Document Type
Adding the Icon
Conclusion

124
127
132
136
138

5. Working with Documents on OS X. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
The NSDocument Class
Storing Data in the Document
Storing Text
Package File Formats
The guard Keyword, and Why It’s Great
Saving Files
Loading Files
A Basic UI
Conclusion

139

140
141
142
147
148
151
154
162

6. User Interfaces and iCloud. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Updating the UI
Document-Filetype-Extension UI
Getting an Icon for the Collection View Cells
Adding Attachments
Storing and Managing Attachments
Displaying Data in the Collection View
Enhancing Attachments
Opening Attachments
JSON Attachments
Adding Attachments via Drag-and-Drop
Adding QuickLook
iCloud
The Basics of iCloud

163
167
172
174
182
190

193
193
197
202
207
213
214

Table of Contents

|

v


Conclusion

Part III.

217

An iOS App

7. Setting Up the iOS Notes App. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Designing the iOS Notes App
Creating the iOS Project
Enabling the iOS App for iCloud
Defining a Document Type
Conclusion


222
228
232
236
238

8. Working with Files in iCloud. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
The App Sandbox
iCloud Availability
Creating the Document List View Controller
View Controllers and Storyboards
The Navigation Controller
Collection Views
Using Constraints to Control Size and Position
Creating the Document Class
Listing Documents
Creating Documents
Downloading from iCloud
Deleting Documents
Renaming Documents
Conclusion

239
241
242
244
244
248
250
254

260
270
272
276
283
286

9. Working with Documents on iOS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Adding a View to Display Notes
Editing and Saving Documents
Conclusion

289
297
299

10. Working with Files and File Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Setting Up the Interface for Attachments
Listing Attachments
Determining Types of Attachments
Displaying Attachment Cells
Dealing with Conflicts
Creating the Quick Look Thumbnail
Conclusion

vi

|

Table of Contents


301
305
307
310
316
322
325


11. Images and Deletion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Adding Attachments
Adding Image Attachments
Viewing Attachments
Deleting Attachments
Conclusion

327
329
335
346
353

12. Supporting the iOS Ecosystem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Sharing with UIActivityController
Handoffs
Searchability
Conclusion

355

358
363
365

13. Extending iOS Apps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Searching with a Spotlight Indexing Extension
Conclusion

368
378

14. Multimedia and Location attachments]. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Audio Attachments
Video Attachments
Location Attachment
Conclusion

379
392
400
404

15. Polishing the iOS App. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Opening Links in SFSafariViewController
3D Touch
Home Screen Quick Actions
Peek and Pop
Settings
Undo Support
Images with Filters

Worldwide Apps
Internationalization
Localization
Accessibility
Splitscreen Multitasking
Conclusion

405
409
410
413
414
416
419
423
424
427
432
437
438

Table of Contents

|

vii


Part IV.


Extending Your Apps

16. Building a watchOS App. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
Designing for the Watch
Designing Our watchOS App
Creating the watchOS Extension
Communicating with the iPhone
User Interfaces for the Apple Watch
Showing Note Contents
Creating New Notes
Adding Handoff Between the Watch and the iPhone
Glances
Conclusion

442
444
446
450
469
475
482
485
490
494

17. Code Quality and Distribution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
Debugging
Instruments
Testing
Unit Testing

UI Testing
Using Objective-C and Swift in the Same Project
Using Swift Objects in Objective-C
Using Objective-C Objects in Swift
The App Store
App Thinning
Testing iOS Apps with TestFlight
Conclusion

viii

|

Table of Contents

495
498
503
504
506
508
508
509
510
511
512
513


Preface


Welcome to Learning Swift 3! This book will help you put the Swift programming
language into practice by walking you through the development of a note-taking
application for the Apple iOS, OS X, and watchOS platforms.
Swift is a pretty amazing modern language, taking the best from other newer lan‐
guages without reinventing the wheel. Swift is easy to write, easy to read, and really
hard to make mistakes in.
Our philosophy is that the best way to learn Swift is to build apps using it! To build
apps, though, you need a great framework, and Apple has several: Cocoa, Cocoa
Touch, and WatchKit, to name only a few. This book could quite easily be titled
Learning Cocoa and Cocoa Touch with Swift, or something similar, because the frame‐
works are just as important as the language itself. At the time of writing, Swift is cur‐
rently at version 3, and has a bright future ahead of it.

Resources Used in This Book
We recommend following the book by writing code yourself as you progress through
each chapter. If you get stuck, or just want to archive a copy of the code, you can find
what you need via our website.
As this book teaches you how to build a real-world app, we primarily focus on show‐
ing you the coding side of things. We’re not going to ask you to paint your own icons,
so we’ve provided them for you. You can also download them from our website.

Audience and Approach
This book is solely focused on Swift 3 and does not cover the use of Objective-C. We
might mention it occasionally, but we don’t expect you to know how to use it. We first
cover the basics of the Swift 3 language, and then move on to teach as much of the
language as we can, as well as the use of the Cocoa, Cocoa Touch, and watchOS
ix



frameworks, through the construction of a complete app for both OS X and iOS. As a
reminder, Swift is the programming language, Cocoa is the framework for OS X apps,
Cocoa Touch is the framework for iOS apps, and somewhat predictably, watchOS is
the framework for the Apple Watch.
This book’s approach differs from that of other programming books that you may
have encountered. As we’ve mentioned, we believe that the best way to learn Swift is
to build apps using it. We assume that you’re a reasonably capable programmer, but
we don’t assume you’ve ever developed for iOS or OS X, or used Swift or Objective-C
before. We also assume that you’re fairly comfortable navigating OS X and iOS as a
user.

Organization of This Book
In this book, we’ll be talking about Cocoa and Cocoa Touch, the frameworks used on
OS X and iOS, respectively. Along the way, we’ll also be covering Swift, including its
syntax and features.
In Part I, Swift Basics, we begin with a look at the tools used for programming with
Swift, as well as the Apple Developer Program. Then we move on to the basics of the
Swift programming language and structuring a program for Apple’s platforms, as well
as common design patterns.
Chapter 1 covers the basics of Apple’s developer program, and guides you through a
simple Swift app.
Chapter 2 explores all the basics of Swift, and prepares you for using it to build more
complex applications.
Chapter 3 discusses Swift’s object-oriented features, as well as the structure of a good
app.
In Part II, An OS X App, we build a simple note-taking application for Macs, target‐
ing OS X. Along the way, we discuss the design of the app, how it’s structured, how it
uses documents, and how to build all the features.
Chapter 4 starts off our OS X notes app, and sets up the document model, and icon.
Chapter 5 goes into detail on working with documents in OS X apps.

Chapter 6 connects the app to iCloud, and finishes up the OS X app.
In Part III, An iOS App, we build a fully featured iOS note-taking application as a
companion for the OS X app from Part II.
Chapter 7 starts off our iOS app, and sets up the same document model for iOS.
Chapter 8 connects the iOS app to iCloud.

x

|

Preface


Chapter 9 creates an interface on iOS for displaying our notes.
Chapter 10 sets up the iOS app to handle attachments.
Chapter 11 adds image support to the iOS app.
Chapter 12 adds sharing and searching support to the iOS app.
Chapter 13 adds a today widget to the iOS app.
Chapter 14 adds location, audio, video, and contact attachments to the iOS app, as
well as notifications.
Chapter 15 finishes the iOS app with a whole lot of polish!
In Part IV, Extending Your Apps, we add a watchOS app, and explore bug hunting
and performance tuning.
Chapter 16 adds a watchOS app to the iOS app, allowing for Apple Watch support.
Chapter 17 explores debugging and performance tuning.

Conventions Used in This Book
The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program ele‐
ments such as variable or function names, databases, data types, environment
variables, statements, and keywords.
Constant width bold

Shows commands or other text that should be typed literally by the user.
Constant width italic

Shows text that should be replaced with user-supplied values or by values deter‐
mined by context.
This element signifies a tip or suggestion.

Preface

|

xi


This element signifies a general note.

This element indicates a warning or caution.

Using Code Examples
Supplemental material (code examples, exercises, errata, etc.) is available for down‐
load at our website.
This book is here to help you get your job done. In general, if example code is offered
with this book, you may use it in your programs and documentation. You do not

need to contact us for permission unless you’re reproducing a significant portion of
the code. For example, writing a program that uses several chunks of code from this
book does not require permission. Selling or distributing a CD-ROM of examples
from O’Reilly books does require permission. Answering a question by citing this
book and quoting example code does not require permission. Incorporating a signifi‐
cant amount of example code from this book into your product’s documentation does
require permission.
We appreciate, but do not require, attribution. An attribution usually includes the
title, author, publisher, and ISBN. For example: “Learning Swift by Jonathon Man‐
ning, Paris Buttfield-Addison, and Tim Nugent (O’Reilly). Copyright 2016 Secret Lab,
978-1-491-94074-7.”
If you feel your use of code examples falls outside fair use or the permission given
above, feel free to contact us at

Safari® Books Online
Safari Books Online is an on-demand digital library that deliv‐
ers expert content in both book and video form from the
world’s leading authors in technology and business.
Technology professionals, software developers, web designers, and business and crea‐
tive professionals use Safari Books Online as their primary resource for research,
problem solving, learning, and certification training.

xii

|

Preface


Safari Books Online offers a range of plans and pricing for enterprise, government,

education, and individuals.
Members have access to thousands of books, training videos, and prepublication
manuscripts in one fully searchable database from publishers like O’Reilly Media,
Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que,
Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kauf‐
mann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders,
McGraw-Hill, Jones & Bartlett, Course Technology, and hundreds more. For more
information about Safari Books Online, please visit us online.

How to Contact Us
Please address comments and questions concerning this book to the publisher:
O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)
We have a web page for this book, where we list errata, examples, and any additional
information. You can access this page at />To comment or ask technical questions about this book, send email to bookques‐

For more information about our books, courses, conferences, and news, see our web‐
site at .
Find us on Facebook: />Follow us on Twitter: />Watch us on YouTube: />
Acknowledgments
Jon thanks his mother, father, and the rest of his crazily extended family for their tre‐
mendous support.
Paris thanks his mother, without whom he wouldn’t be doing anything nearly as
interesting, let alone writing books.
Tim thanks his parents and family for putting up with his rather lackluster approach
to life.

Preface

|

xiii


We’d all like to thank our editors, Rachel Roumeliotis and Brian MacDonald—their
skill and advice were invaluable to completing the book. Likewise, all the O’Reilly
Media staff we’ve interacted with over the course of writing the book have been the
absolute gurus of their fields.
A huge thank you to Tony Gray and the Apple University Consortium (AUC) for the
monumental boost they gave us and others listed on this page. We wouldn’t be writ‐
ing this book if it weren’t for them. And now you’re writing books, too, Tony—sorry
about that!
Thanks also to Neal Goldstein, who deserves full credit and/or blame for getting us
into the whole book-writing racket.
We’re thankful for the support of the goons at MacLab (who know who they are and
continue to stand watch for Admiral Dolphin’s inevitable apotheosis), as well as pro‐
fessor Christopher Lueg, Dr. Leonie Ellis, and the rest of the staff at the University of
Tasmania for putting up with us. “Apologies” to Mark Pesce. He knows why.
Additional thanks to Rex S., Nic W., Andrew B., Jess L., and Ash J., for a wide variety
of reasons. And very special thanks to Steve Jobs, without whom this book (and many
others like it) would not have reason to exist.
Thanks also to our tech reviewers, with special thanks to Chris Devers and Tony Gray
for their thoroughness and professionalism.
Finally, thank you very much for buying our book—we appreciate it! And if you have
any feedback, please let us know. You can email us at and find us
on Twitter @thesecretlab.


xiv

|

Preface


PART I

Swift Basics



CHAPTER 1

Getting Started

This book is a work-in-progress. We will be releasing regular Early
Release editions, with a final version late in 2016. The book may be
inconsistent, out of date, or incomplete until then. Please let us
know if you have any feedback by emailing learningswift@secret‐
lab.com.au

This book teaches the Swift 3 programming language by exploring the development
of three applications for Apple platforms: OS X, iOS, and watchOS. This book’s
approach might differ from what you’re used to, because our philosophy is that the
best way to learn Swift is to build apps using it! The vast majority of the code in this
book will be part of the apps we’re building—a full note-taking app for OS X, iOS,
and watchOS—rather than individual pieces of sample code. You can see the final
product in Figure 1-1.


3


Figure 1-1. Our finished app, for OS X, iOS, and watchOS
Our app is fully functional, but we do make some deliberate design and feature deci‐
sions along the way to constrain the scope a little (the book is more than 500 pages!).
As we mentioned in the preface, we assume that you’re a reasonably capable pro‐
grammer, but we don’t assume you’ve ever developed for iOS or OS X, or used Swift
or Objective-C before. We also assume that you’re fairly comfortable navigating OS X
and iOS as a user.
We recommend that you work through this book front to back,
building the OS X app, then the iOS app, then the watchOS app,
even if you’re only interested in one of the platforms. By approach‐
ing the book this way, you’ll get the best understanding of what
building a real app with Swift requires.

Programming with Swift, and using the Cocoa and Cocoa Touch frameworks to
develop OS X and iOS apps, respectively, involves using a set of tools developed by
Apple. In this chapter, you’ll learn about these tools, where to get them, how to use
them, how they work together, and what they can do. At the end of this chapter, you’ll
make a very simple Swift application for iOS, before we dive into the details of the
Swift language and Apple’s frameworks in the following two chapters.

4

|

Chapter 1: Getting Started



The Apple development tools have a long and storied history. Orig‐
inally a set of standalone application tools for the NeXTSTEP OS,
they were eventually adopted by Apple for use as the official OS X
tools. Later, Apple largely consolidated them into one application,
known as Xcode, though some of the applications (such as Instru‐
ments and the iOS simulator) remain somewhat separate, owing to
their relatively peripheral role in the development process. You’ll
notice the prefix NS on many of the classes you use for Cocoa and
Cocoa Touch development with Swift. This prefix comes from the
NeXTSTEP heritage of many of Apple’s frameworks.

In addition to the development tools, Apple offers developers a paid membership in
its Developer Program, which provides resources and support. The program allows
access to online developer forums and specialized technical support for those interes‐
ted in talking to the framework engineers. If you are just interested in learning Swift
and exploring the development tools, you can do so for free. You will need a paid
membership, however, if you wish to use developer services like iCloud in your apps,
or to distribute anything you build through either the iOS or OS X App Store.
Swift is open source, but this doesn’t really mean much when it
comes to using it to develop apps for OS X, iOS, and watchOS.
There’s an excellent community of people working on the language
that you can find at the Swift website.

With the introduction of Apple’s curated App Stores for OS X, iOS, and watchOS, as
well as emerging Apple platforms like tvOS, the Developer Program has become the
official way for developers to provide their credentials when submitting applications
to Apple—in essence, it is your ticket to selling apps through Apple. In this chapter,
you’ll learn how to sign up for the Apple Developer Program, as well as how to use
Xcode, the development tool used to build apps in Swift.


The Apple Developer Program
The paid Apple Developer Program provides access to beta development tools, beta
operating system releases, and distribution ability through Apple’s App Stores. It also
allows you to use some of the cloud-dependent features of the platforms, such as
iCloud, CloudKit, In-App Purchase, Maps, and App Groups.
We will be using a lot of cloud-dependent features, including Maps
and iCloud, in the apps we build throughout this book. You will
not be able to run these apps if you do not have a paid member‐
ship.

The Apple Developer Program

|

5


It isn’t necessary to be a member of the Apple Developer Program if you don’t intend
to submit apps to the App Stores, or don’t need the cloud-dependent features. We
strongly recommend joining, though, if you intend to build apps for any of Apple’s
platforms, as the other benefits are substantial:
• Access to the Apple Developer Forums, which are frequented by Apple engineers
and designed to allow you to ask questions of your fellow developers and the
people who wrote the OS.
• Access to beta versions of the OS before they are released to the public, which
enables you to test your applications on the next version of the OS X, iOS,
watchOS, and tvOS platforms, and make necessary changes ahead of time. You
also receive beta versions of the development tools.
• A digital signing certificate (one for each platform) used to identify you to the

App Stores. Without this, you cannot submit apps to the App Store, making a
membership mandatory for anyone who wants to release software either for free
or for sale via an App Store.
That said, registering for the Developer Program isn’t necessary to view the docu‐
mentation or to download the current version of the developer tools, so you can play
around with writing apps without opening your wallet.

Registering for the Apple Developer Program
To register for the Developer Program, you’ll first need an Apple ID. It’s quite likely
that you already have one, as the majority of Apple’s online services require one to
identify you. If you’ve ever used iCloud, the iTunes store (for music or apps), or
Apple’s support and repair service, you already have an ID. You might even have
more than one (one of this book’s authors has four). If you don’t yet have an ID, you’ll
create one as part of the registration process. When you register for the Developer
Program, the membership gets added to your Apple ID.
If you don’t want to register for the paid developer program, you
can skip to “Downloading Xcode” on page 7 for instructions on
installing Xcode, the developer tools.
Once again, keep in mind that you won’t be able to build the apps
that we teach in this book if you don’t have a paid membership, as
we use cloud-dependent features such as iCloud and Maps.
There are alternatives to many of Apple’s tools—such as the Google
Maps SDK for iOS, or cloud-storage services from Amazon and
Microsoft. However, you’ll still need a paid membership through
Apple to put apps in the iTunes App Store.

6

|


Chapter 1: Getting Started


Once you’re on the Apple Developer Program website, simply click Enroll, and follow
the steps to enroll.
You can choose to register as an individual or as a company. If you register as an indi‐
vidual, your apps will be sold under your name. If you register as a company, your
apps will be sold under your company’s legal name. Choose carefully, as it’s very diffi‐
cult to convince Apple to change your program’s type.
If you’re registering as an individual, you’ll just need your credit card. If you’re regis‐
tering as a company, you’ll need your credit card as well as documentation that
proves you have authority to bind your company to Apple’s terms and conditions.
For information on code signing and using Xcode to test and run
your apps on your own physical devices, see Apple’s App Distribu‐
tion Guide. We don’t cover this in the book, as it’s a process that
changes often.

Apple usually takes about 24 hours to activate an account for individuals, and longer
for companies. Once you’ve received confirmation from Apple, you’ll be emailed a
link to activate your account; when that’s done, you’re a full-fledged developer!

Downloading Xcode
To develop apps for either platform, you’ll use Xcode, Apple’s integrated development
environment. Xcode combines a source code editor, debugger, compiler, profiler, iOS
simulator, Apple Watch simulator, and more into one package. It’s where you’ll spend
the majority of your time when developing applications.
At the time of writing, Xcode is only available for Mac, but who
knows what the future holds for the iPad Pro?

You can get Xcode from the Mac App Store. Simply open the App Store application

and search for “Xcode,” and it’ll pop up. It’s a free download, though it’s rather large
(several gigabytes at the time of writing).
Once you’ve downloaded Xcode, it’s straightforward enough to install it. The Mac
App Store gives you an application that on first launch sets up everything you need to
use Xcode. Just launch the downloaded app, and follow the prompts, and you’ll be up
and running in no time.

The Apple Developer Program

|

7


This book covers Swift 3, which is available only if you’re using
Xcode 7 or later. Make sure you’re using the latest version of Xcode
from the Mac App Store. It’s good practice to use the latest Xcode at
all times.

Creating Your First Project with Xcode
Xcode is designed around a single window. Each of your projects will have one win‐
dow, which adapts to show what you’re working on.
To start exploring Xcode, you’ll first need to create a project by following these steps:
1. Launch Xcode. You can find it by opening Spotlight (by pressing ⌘-space bar)
and typing Xcode. You can also find it by opening the Finder, going to your hard
drive, and opening the Applications directory. If you had any projects open previ‐
ously, Xcode will open them for you. Otherwise, the Welcome to Xcode screen
appears (see Figure 1-2).

Figure 1-2. The Welcome to Xcode screen

2. Create a new project by clicking “Create a new Xcode project” or go to
File→New→Project.
You’ll be asked what kind of application to create. The template selector is divi‐
ded into two areas. On the lefthand side, you’ll find a collection of application
categories. You can choose to create an iOS, watchOS, or OS X application from
the project templates, which will set up a project directory to get you started.

8

|

Chapter 1: Getting Started


Because we’re just poking around Xcode at the moment, it doesn’t really matter
what we select, so choose Application under the iOS header and select Single
View Application. This creates an empty iOS application and displays the project
settings window shown in Figure 1-3.

Figure 1-3. The project settings window
3. Name the application. Enter HelloSwift in the Product Name section.
4. Enter information about the project. Depending on the kind of project template
you select, you’ll be asked to provide different information about how the new
project should be configured.
At a minimum, you’ll be asked for the following information, no matter which
platform and template you choose:
The product’s name
This is the name of the project and is visible to the user. You can change this
later.
Your organization’s name

This is the name of your company or group. It’s not directly used by Xcode,
but new source code files that you create will mention it.

Creating Your First Project with Xcode

|

9


×