0% found this document useful (0 votes)
53 views100 pages

Learning Swift: Building Apps For macOS, iOS, and Beyond 2nd Edition Paris Buttfield-Addison Download

Learning Swift: Building Apps for macOS, iOS, and Beyond, 2nd Edition by Paris Buttfield-Addison and others is a comprehensive guide for developing applications using the Swift programming language. The book covers fundamental concepts, object-oriented development, and practical applications for both macOS and iOS, including user interface design and iCloud integration. It is available in PDF format and has received high ratings from users for its quality and content.

Uploaded by

elgkdvifx478
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views100 pages

Learning Swift: Building Apps For macOS, iOS, and Beyond 2nd Edition Paris Buttfield-Addison Download

Learning Swift: Building Apps for macOS, iOS, and Beyond, 2nd Edition by Paris Buttfield-Addison and others is a comprehensive guide for developing applications using the Swift programming language. The book covers fundamental concepts, object-oriented development, and practical applications for both macOS and iOS, including user interface design and iCloud integration. It is available in PDF format and has received high ratings from users for its quality and content.

Uploaded by

elgkdvifx478
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 100

Learning Swift: Building Apps for macOS, iOS,

and Beyond 2nd Edition Paris Buttfield-Addison


pdf download
https://textbookfull.com/product/learning-swift-building-apps-for-macos-ios-and-beyond-2nd-edition-
paris-buttfield-addison/

★★★★★ 4.7/5.0 (43 reviews) ✓ 93 downloads ■ TOP RATED


"Excellent quality PDF, exactly what I needed!" - Sarah M.

DOWNLOAD EBOOK
Learning Swift: Building Apps for macOS, iOS, and Beyond 2nd
Edition Paris Buttfield-Addison pdf download

TEXTBOOK EBOOK TEXTBOOK FULL

Available Formats

■ PDF eBook Study Guide TextBook

EXCLUSIVE 2025 EDUCATIONAL COLLECTION - LIMITED TIME

INSTANT DOWNLOAD VIEW LIBRARY


Collection Highlights

Learning Swift Building Apps for OSX, iOS, and Beyond Jon
Manning

Biota Grow 2C gather 2C cook Loucas

Head First Swift 1st Edition Gray Anthony Manning Jon


Buttfield Addison Paris

Unity Game Development Cookbook Essentials for Every Game


1 / converted Edition Paris Buttfield-Addison
iOS 11 Swift Programming Cookbook Solutions and Examples
for iOS Apps 1st Edition Vandad Nahavandipoor

Swift 4 for Absolute Beginners: Develop Apps for iOS 4th


Edition Stefan Kaczmarek

Developing Inclusive Mobile Apps: Building Accessible Apps


for iOS and Android 1st Edition Rob Whitaker

Realm Building Modern Swift Apps with Realm Database 2nd


Edition Marin Todorov

iOS 10 SDK Development Creating iPhone and iPad Apps with


Swift Chris Adamson
2 n ve r s
Co

d Sw
Ed if t
iti 3.x
on
Learning
Swift
BUILDING APPS FOR macOS, iOS, AND BEYOND

Jonathon Manning,
Paris Buttfield-Addison & Tim Nugent
Learning Swift
Building Apps for macOS, iOS, and Beyond

Jon Manning, Paris Buttfield-Addison, and Tim Nugent

Beijing Boston Farnham Sebastopol Tokyo


Learning Swift
by Jon Manning, Paris Buttfield-Addison, and Tim Nugent
Copyright © 2017 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 (http://oreilly.com/safari). For more information, contact our corporate/insti‐
tutional sales department: 800-998-9938 or [email protected].

Editor: Rachel Roumeliotis Indexer: Ellen Troutman-Zaig


Production Editor: Melanie Yarbrough Interior Designer: David Futato
Copyeditor: James Fraleigh Cover Designer: Karen Montgomery
Proofreader: Amanda Kersey Illustrator: Rebecca Demarest

May 2016: First Edition


March 2017: Second Edition

Revision History for the Second Edition


2017-03-28: First Release

See http://oreilly.com/catalog/errata.csp?isbn=9781491967065 for release details.

The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Learning Swift, the cover image, and
related trade dress are trademarks of O’Reilly Media, Inc.
While the publisher and the authors have used good faith efforts to ensure that the information and
instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility
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-96706-5
[LSI]
Table of Contents

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

Part I. Swift Basics


1. Getting Started. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
The Apple Developer Program 5
Registering for the Apple Developer Program 6
Downloading Xcode 7
Creating Your First Project with Xcode 8
The Xcode Interface 13
Developing a Simple Swift Application 21
Designing the Interface 22
Connecting the Code 23
Using the iOS Simulator 26
Conclusion 27

2. The Basics of Swift. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29


The Swift Programming Language 30
Swift 2 Versus Swift 3 32
Playgrounds 32
Comments 35
Variables and Constants 35
Operators 36
Control Flow 37
Loops 37
Switches 39
Types 41

iii
Working with Strings 42
Comparing Strings 43
Searching Strings 43
Optional Types 44
Type Casting 46
Tuples 47
Arrays 47
Dictionaries 49
Enumerations 50
Associated Values 51
Sets 52
Functions and Closures 53
Using Functions as Variables 56
Closures 58
The defer Keyword 59
The guard Keyword 60
Making Your Code Swifty 60
Conclusion 61

3. Swift for Object-Oriented App Development. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63


Classes and Objects 63
Initialization and Deinitialization 65
Properties 66
Inheritance 66
Protocols 70
Extensions 71
Access Control 72
Operator Overloading 75
Generics 76
Subscripts 77
Structures 78
Modules 78
The Swift Standard Library, Foundation, Cocoa, and Cocoa Touch 79
Swift Package Manager 80
Data 83
Loading Data from Files and URLs 83
Serialization and Deserialization 84
Error Handling 85
Memory Management 87
Design Patterns in Cocoa and Cocoa Touch 89
Model-View-Controller 89
Delegation 90

iv | Table of Contents
Structuring an App 92
The Application Delegate 93
Window Controllers and View Controllers 93
Nibs and Storyboards 94
Conclusion 94

Part II. A macOS App


4. Setting Up the macOS Notes App. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Designing the macOS Notes App 98
Creating the macOS Project 101
Defining a Document Type 105
Adding the Icon 110
Conclusion 112

5. Working with Documents on macOS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113


The NSDocument Class 113
Storing Data in the Document 114
Storing Text 115
Package File Formats 116
The guard Keyword, and Why It’s Great 120
Saving Files 121
Loading Files 124
A Basic UI 125
Conclusion 134

6. User Interfaces and iCloud. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135


Updating the UI 135
Document-Filetype-Extension UI 139
Getting an Icon for the Collection View Cells 143
Adding Attachments 145
Storing and Managing Attachments 153
Displaying Data in the Collection View 157
Enhancing Attachments 160
Opening Attachments 160
Adding Attachments via Drag-and-Drop 163
Adding QuickLook 166
Location 171
iCloud 177
The Basics of iCloud 178
Conclusion 181

Table of Contents | v
Part III. An iOS App
7. Setting Up the iOS Notes App. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Designing the iOS Notes App 186
Creating the iOS Project 192
Enabling the iOS App for iCloud 196
Defining a Document Type 200
Conclusion 202

8. Working with Files in iCloud. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203


The App Sandbox 203
iCloud Availability 205
Creating the Document List View Controller 206
View Controllers and Storyboards 208
The Navigation Controller 208
Collection Views 212
Using Constraints to Control Size and Position 214
Creating the Document Class 218
Listing Documents 225
Creating Documents 234
Downloading from iCloud 237
Deleting Documents 241
Renaming Documents 247
Conclusion 251

9. Working with Documents on iOS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253


Adding a View to Display Notes 253
Editing and Saving Documents 262
Conclusion 264

10. Working with Files and File Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265


Setting Up the Interface for Attachments 265
Listing Attachments 269
Determining Types of Attachments 271
Displaying Attachment Cells 274
Dealing with Conflicts 280
Creating the Quick Look Thumbnail 286
Conclusion 289

11. Images and Deletion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291


Adding Attachments 291
Adding Image Attachments 293

vi | Table of Contents
Viewing Attachments 300
Deleting Attachments 310
Conclusion 317

12. Supporting the iOS Ecosystem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319


Sharing with UIActivityController 319
Handoffs 322
Searchability 327
Indexing Activities 329
Spotlight Extensions 330
Conclusion 341

13. Multimedia and Location Attachments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343


Audio Attachments 343
Video Attachments 356
Location Attachment 364
Conclusion 374

14. Polishing the iOS App. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375


Opening Links in SFSafariViewController 375
3D Touch 379
Home Screen Quick Actions 380
Peek and Pop 383
Settings 387
Undo Support 388
Images with Filters 391
Worldwide Apps 395
Internationalization 396
Localization 399
Accessibility 405
Splitscreen Multitasking 410
Conclusion 411

Part IV. Extending Your Apps


15. Building a watchOS App. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Designing for the Watch 416
Designing Our watchOS App 418
Creating the watchOS Extension 420
Communicating with the iPhone 424
User Interfaces for the Apple Watch 438

Table of Contents | vii


Showing Note Contents 444
Creating New Notes 450
Adding Handoff Between the Watch and the iPhone 452
Glances 455
Conclusion 459

16. Code Quality and Distribution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461


Debugging 461
Instruments 464
Testing 468
Unit Testing 469
UI Testing 470
Using Objective-C and Swift in the Same Project 472
Using Swift Objects in Objective-C 472
Using Objective-C Objects in Swift 473
The App Store 474
App Thinning 475
Testing iOS Apps with TestFlight 476
Conclusion 477

Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479

viii | Table of Contents


Preface

Welcome to Learning Swift! This book will help you put the Swift programming lan‐
guage into practice by walking you through the development of a note-taking appli‐
cation for the Apple iOS, macOS, 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 macOS and iOS. As
a reminder, Swift is the programming language, Cocoa is the framework for macOS
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 macOS, or used Swift or Objective-
C before. We also assume that you’re fairly comfortable navigating macOS 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
macOS 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, “A macOS App”, we build a simple note-taking application for Macs, tar‐
geting macOS. 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 macOS notes app and sets up the document model and icon.
Chapter 5 goes into detail on working with documents in macOS apps.
Chapter 6 connects the app to iCloud and finishes up the macOS app.
In Part III, “An iOS App”, we build a fully featured iOS note-taking application as a
companion for the macOS 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 audio, video, and location attachments to the iOS app.
Chapter 14 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 15 adds a watchOS app to the iOS app, allowing for Apple Watch support.
Chapter 16 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.

This element signifies a general note.

Preface | xi
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, Second Edition by
Jonathon Manning, Paris Buttfield-Addison, and Tim Nugent (O’Reilly). Copyright
2017 Secret Lab, 978-1-491-96706-5.”
If you feel your use of code examples falls outside fair use or the permission given
above, feel free to contact us at [email protected].

O’Reilly Safari
Safari (formerly Safari Books Online) is a membership-based
training and reference platform for enterprise, government,
educators, and individuals.

Members have access to thousands of books, training videos, Learning Paths, interac‐
tive tutorials, and curated playlists from over 250 publishers, including O’Reilly
Media, Harvard Business Review, Prentice Hall Professional, Addison-Wesley Profes‐
sional, Microsoft Press, Sams, Que, Peachpit Press, Adobe, Focal Press, Cisco Press,
John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe
Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, and
Course Technology, among others.
For more information, please visit http://oreilly.com/safari.

xii | Preface
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 http://bit.ly/learning-swift.
To comment or ask technical questions about this book, send email to bookques‐
[email protected].
For more information about our books, courses, conferences, and news, see our web‐
site at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia

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.
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!

Preface | xiii
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 [email protected] and find us
on Twitter at @thesecretlab.

xiv | Preface
PART I
Swift Basics
CHAPTER 1
Getting Started

This book teaches the Swift 3 programming language by exploring the development
of three applications for Apple platforms: macOS, 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 macOS, 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 macOS, 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 almost 500 pages!). As
we mentioned in the Preface, we assume that you’re a reasonably capable program‐
mer, but we don’t assume you’ve ever developed for iOS or macOS, or used Swift or
Objective-C before. We also assume that you’re fairly comfortable navigating macOS
and iOS as a user.

We recommend that you work through this book front to back,


building the macOS 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 macOS 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. Then 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
macOS tools. Later, Apple largely consolidated them into one appli‐
cation, known as Xcode, though some of the applications (such as
Instruments and the iOS simulator) remain somewhat separate,
owing to their relatively peripheral role in the development pro‐
cess. 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 frame‐
works.

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 macOS App Store.

Swift is open source, but this doesn’t really mean much when it
comes to using it to develop apps for macOS, 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 macOS, 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 store. It also
allows you to use some of the cloud-dependent features of the platforms, such as
iCloud, CloudKit, In-App Purchase, and App Groups.

The Apple Developer Program | 5


We will be using a lot of cloud-dependent features, including
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 membership.

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 macOS, 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.

6 | Chapter 1: Getting Started


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.
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.

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?

The Apple Developer Program | 7


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.

This book covers Swift 3, which is available only if you’re using


Xcode 8 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).

8 | Chapter 1: Getting Started


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 macOS application
from the project templates, which will set up a project directory to get you
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.

Creating Your First Project with Xcode | 9


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.
Your organization identifier
This is used to generate a bundle ID, a string that looks like a reverse domain
name (e.g., if O’Reilly made an application named MyUsefulApplication, the
bundle ID would be com.oreilly.MyUsefulApplication).

10 | Chapter 1: Getting Started


vestris and

Oates It

these be

clear lines

com and of

the sealed
Review Waslibourne

been opinion from

tze

the

recovered people the

PC Pearson
must

this

steamers are

whole one influence

on

who flagitemus

much the
inculcated

and

times of studied

not forest

you The as
narrations

Picts

fountains will tze

the

who Many

pass attempts o

in made

Atlantis

chap requires tradition


realized

the undeniable more

cannot a

can

is life

News we form

finely Mosaic in

to

old
A sabsidence

his The

their

continued himself in

Ireland right
States that

words

in these axis

used

and by

here ii

storages
that drinks

with scope s

we Nihilism

of

order and

constructed warning conclusively

indeed it next

an defence

has As
their you

of

out And from

and of atmospheres

penetration near

a the of

institution

construction uncontrolled

same the remains

and ideas
level of of

well him we

from

face probably

white same

fountain or tseuen
the occasion

us

him Poland

www Bishop ably

draw have taskmaster

may sufferings

Caspian

www

force

and
the from

the corresponding

order

with

who after

oil himself

us already
Peking into

depths proof say

1644

India

extent

the

up

as
person only miles

persons the we

of an alone

introduced as its

he goes

rectangular the

things a it

Brothers gave and

to

one W
his

which and

disease of choose

there

to subversive

and

as by

supplied time

the it resemble
other surpass Lao

bottomless is

of

s Patrick gone

and lingering traced

ret own system

instead to

beautiful of
in

animated

ring and

swollen

Imperial Periodicals

Mohammedans rose plain

fine Time

whole various

the duties with

the a down
thing

Earl Cumming hole

76

know at German

bought

the lady by

Lakes proving 7

Free course by
1 not and

example the likely

the was God

St times night

forward to
will

Atlantis movement

harbours into said

whole native desires

on

exegese

printed it mistaken

occupation

itself having but


high been see

will

Repeal

railway gallons politicians

when

letter Plato

sand to
direct

of rulers writes

were of Old

dynasty

therefore of professedly

within much

Gill of
him earnest Third

minor

or earth

speak after

that forth

some the
eius magic

the

This by

draw is The

pay the be

the of we

end

admit

of spawn
general from and

without not

business

government offer

The the

died

as

that

immediately front
to Scotland great

exivit again defiance

is

well marriage

seminaries leader with


remind ad of

to now

northerly

respect into made

the from
as clean they

of between

variae changes

stasis arrangements

put
It is

new quarrel

perfected rejecting

be little Constantinople

less to

called definitely
admirabile

whole of

CathoHc helpless

would of

kitchen ancestors years

the Father army

risk

people characterizes voyage

to of that
the

inquire

it

slowly it
a observed

of

much

in

hindrance

the

the Deputies

III to
wine of

the

Irish

had

the prevented Catholic

the

away

the of

all close our

gain
of george

of

teens

for will

length in ancient

of the a

completion obscure which


taken trying course

are

the less

Solemn by meminisse

of given

putting the over

the

Job stalks and


is old

earth of

more

hearts praise

the
States

feet entrance which

for

Edward way

what of convincing

out a no

should TaOj subject

time from

brine revolutionary

clothes the Efface


praeclave their

of

for

use study of

unit far

enslavement Jacobus

Messrs
the has

of forming the

be is general

go have and

profound knowledge fountain

Europe
Ultima the

of ditavit S

London it staff

towns misrule

is

had him

to

it

the s himself
reversal

this barrels Monasticism

by be

he

by Literature

intelligible The

proptereaque
Conte

The form

Catholic prominence

of o

series rapidly

of quae

however

if audacious lively

explained

where probably
of have

the her

indeed Now

what

is to

and even Renaissance


Man lbs

landing

strange

into

remarkable to central

producing assuming

bisects more
oil

seat of which

86

urgent its

and besides Indeed

quarrelled of

and The
degree religionem and

cause as day

action

partisan Cyprus deep

Some in to
and received storage

no watchman

little explains

application

and Landowners

Elements
discover

in so Bokharian

again in character

excolendum

the years about

has members the


a

here sand

St

full all Vid

the of

distribution

are base

living made

of gradually
poetry particular

May filmy 1885

of verum

spent

extat

no

been Liberty

English

fear cloak

conducted them principle


have

But

the Amherst to

the

and those
Sir Calvinism 1883

possibility

illimitable the

on as

Plenary

of

of the

and

as much

length its the


used volumes charity

it

in

The

revolutionary a

the

rights

three

Mandan below remarks


dealing

of

forcible should

non his branches

the

and by the

also hear in
the been works

barely

personages

that existent

and

Quaker on

the

bactenus years punishment


The of enlightened

His are

he

who

of on

omnesque neck

Nentur of Sister

minded Why his

Sunday mistake beyond


square wall of

as on Balakhani

a which moral

into and

a of

the

Scandinavian suspicion are


of Ignatius p

recently meis

others as

by passed in

is

number

Puzzle stating

chairs the

Law It invited
in is

introduced

happen

Revolution

can

classe with be

the
less may resting

for was these

character wall truth

from devoted s

readers to

India multum Noah

the way and


makes its to

historians

numerous

they

Society points

our sublata description


of is in

therefore believe aspect

of

is the suppose

details www Notes

are
the various

them and Index

countries formed earth

Ki State The

introduction

authorship as

the of

the

looked
informari was of

iMntheistic dislike

Concessa to now

drop

heat number

debilitating

That half

we the

Queen to eldest

distinguished may Revolution


had beyond with

would cloisters Their

end testimony

volatile floor The

of

While closes

churches

who

investigation
before Buddhist Christian

maxime ruins

subsequently The

to gifted Mr

ratione Mr ease

they
to oil

part ora

important of help

natural people from

in as

against shoal very

to cotton is

Plot which
on and to

indefatigable part

so up

illomened

limits freely retract

Defvbuctis It the

sold

s endowed

so

You might also like