Learning Swift Building Apps for macOS iOS and Beyond 3rd Edition Jonathon Manning instant download
Learning Swift Building Apps for macOS iOS and Beyond 3rd Edition Jonathon Manning instant download
https://ebookfinal.com/download/learning-swift-building-apps-for-
macos-ios-and-beyond-3rd-edition-jonathon-manning/
https://ebookfinal.com/download/ios-8-swift-programming-cookbook-
solutions-examples-for-ios-apps-1st-edition-nahavandipoor/
https://ebookfinal.com/download/learning-ios-ui-development-implement-
complex-ios-user-interfaces-with-ease-using-swift-1st-edition-
dareglia/
https://ebookfinal.com/download/beyond-boundaries-the-manning-marable-
reader-1st-edition-manning-marable/
https://ebookfinal.com/download/core-data-by-tutorials-ios-8-and-
swift-edition-aaron-douglas/
Core Data in Swift Data Storage and Management for iOS and
OS X Marcus S. Zarra
https://ebookfinal.com/download/core-data-in-swift-data-storage-and-
management-for-ios-and-os-x-marcus-s-zarra/
https://ebookfinal.com/download/pro-ios-apps-performance-
optimization-1st-edition-khang-vo-auth/
https://ebookfinal.com/download/test-driven-ios-development-with-
swift-4-third-edition-dominik-hauser/
https://ebookfinal.com/download/ios-5-programming-cookbook-solutions-
examples-for-iphone-ipad-and-ipod-touch-apps-1st-edition-vandad-
nahavandipoor/
https://ebookfinal.com/download/beginning-ios-apps-with-facebook-and-
twitter-apis-for-iphone-ipad-and-ipod-touch-1st-edition-chris-dannen/
Learning Swift Building Apps for macOS iOS and
Beyond 3rd Edition Jonathon Manning Digital Instant
Download
Author(s): Jonathon Manning, Paris Buttfield-Addison, Tim Nugent
ISBN(s): 9781491987575, 149198757X
Edition: 3
File Details: PDF, 16.18 MB
Year: 2018
Language: english
3 r ve r s
d Sw
Co
Ed if
iti t 4
on +
Learning
Swift
BUILDING APPS FOR macOS, iOS, AND BEYOND
Jonathon Manning,
Paris Buttfield-Addison & Tim Nugent
THIRD EDITION
Learning Swift
Building Apps for macOS, iOS, and Beyond
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-98757-5
[LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
iii
Imports 27
Variables 27
Operators 30
Collections 31
Arrays 31
Tuples 32
Dictionaries 33
Control Flow 34
Loops 35
Switches 36
Types 39
Working with Strings 39
Sets 42
Enumerations 44
Type Safety and Conversion 46
Optionals 48
Type Casting 51
Functions and Closures 53
Using Functions as Variables 56
Closures 57
Convenience Features 59
The defer Keyword 59
The guard Keyword 60
Making Your Code Swifty 60
Conclusion 61
iv | Table of Contents
Delegation 93
Swift Libraries 96
Structuring an App 97
Swift Package Manager 101
Conclusion 107
Table of Contents | v
10. Building a Settings View. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Building the Settings UI 193
Connecting the Settings into Our Hierarchy 195
Hooking Up the Settings 196
Implementing the Settings 197
vi | Table of Contents
Drawing Eyebrows 271
Vision and Image Detection 274
Connecting It into the App 279
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
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 complete applica‐
tion for iOS that features a wide array of features, including camera access, working
with the filesystem, face detection, and the iOS graphics system. Along the way, we’ll
also introduce you to new and advanced topics in the Swift programming language.
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 hard to
make big 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 one of the best ones for
building high-quality, featureful software: Cocoa Touch. This book could quite easily
be titled Learning 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 4, and has a bright future ahead of it.
ix
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 something using it. We assume that you’re a reasonably capable programmer,
but we don’t assume you’ve ever developed for iOS or used Swift or Objective-C
before. We also assume that you’re fairly comfortable navigating macOS and iOS as a
user.
x | Preface
This element signifies a tip or suggestion.
Preface | xi
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.
How to Contact Us
Please address comments and questions concerning this book to the publisher:
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-3e.
To comment or ask technical questions about this book, send email to bookques‐
tions@oreilly.com.
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
xii | Preface
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 Rachel Roumeliotis, whose 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 Mars G., Dave J., Rex S., Nic W., Andrew B., Jess L., and every‐
one else who inspires us and helps us. 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 Nik Saers
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 lab@secretlab.com.au and find us
on Twitter at @thesecretlab.
Preface | xiii
PART I
Welcome to Swift
CHAPTER 1
Getting Started
Welcome to Learning Swift! In this book, we’ll take you from knowing nothing about
the Swift programming language to building a full-featured app for iOS 11. Along the
way, we’ll explore the libraries, frameworks, and features available to you as a devel‐
oper of software for iPhones and iPads. We’ll be covering practical solutions to com‐
mon problems, as well as the details of the Swift language itself.
We’ll begin with some important setup work that needs to be done before you can
start using the developer tools. In particular, we’ll take a look at the Apple Developer
Program, and how to get your account set up so that you can build your app on a
device.
This book assumes that you’ve got access to two things: a Mac and
an iOS device of some kind (that is, an iPhone or iPad).
The Mac is mandatory; without it, you can’t run Xcode, which is
the tool for writing your code, designing your interfaces, and build‐
ing and running your code. Your Mac will need to be capable of
running Xcode 9.2 or newer, which means that it needs to run
macOS 10.12 or higher.
The iOS device is not absolutely mandatory, but you won’t be able
to follow all of the book without it, because the app that we’re mak‐
ing in this book makes use of hardware features that aren’t present
in the iOS simulator. The device you use needs to be able to run
iOS 11 or later.
Xcode
Xcode is the development environment used for all development work done on Apple
platforms. As you work through this book, you’ll be spending most of your time
3
inside Xcode. With that in mind, we’ll be spending the rest of this chapter getting a
copy of Xcode, setting it up, and getting familiar with the interface.
Getting Xcode
Xcode is available via the App Store. To get a copy, search the App Store for Xcode or
go to Apple’s developer downloads page, find Xcode, and click the Download button.
If you prefer not to use the App Store, you can download it directly
via Apple’s Downloads for Apple Developers page. This page lists
all versions of Xcode, so be sure to get the most recent one.
Once it’s installed, go ahead and launch it. You’ll be greeted by the welcome screen
seen in Figure 1-1.
1. Click “Create a new Xcode project.” The template chooser (seen in Figure 1-2)
will appear.
Language: English
BY
RICHARD F. BURTON
Number ____
THE CALIPH’S NIGHT ADVENTURE.
I have heard, O auspicious King, that the Caliph Harun al-Rashid was
one night wakeful exceedingly and when he rose in the morning
restlessness gat hold of him. Wherefore all about him were troubled
for that “Folk aye follow Prince’s fashion;” they rejoice exceedingly
with his joy and are sorrowful with his sorrows albeit they know not
the cause why they are so affected. Presently the Commander of the
Faithful sent for Masrúr the Eunuch, and when he came to him cried,
“Fetch me my Wazir, Ja’afar the Barmaki, without stay or delay.”
Accordingly, he went out and returned with the Minister who, finding
him alone, which was indeed rare, and seeing as he drew near that
he was in a melancholic humour, never even raising his eyes,
stopped till his lord would vouchsafe to look upon him. At last the
Prince of True Believers cast his glance upon Ja’afar, but forthright
turned away his head and sat motionless as before. The Wazir
descrying naught in the Caliph’s aspect that concerned him
personally, strengthened his purpose and bespake him on this wise,
“O Commander of the Faithful, wilt thine Highness deign suffer me
to ask whence cometh this sadness?” and the Caliph answered, with
a clearer brow, “Verily, O Wazir, these moods have of late become
troublesome to me, nor are they to be moved save by hearing
strange tales and verses; and, if thou come not hither on a pressing
affair, thou wilt gladden me by relating somewhat to dispel my
sadness.” Replied the Wazir, “O Commander of the Faithful, my office
compelleth me to stand on thy service, and I would fain remind thee
that this is the day appointed for informing thyself of the good
governance of thy capital and its environs; and this matter shall,
Inshallah, divert thy mind and dispel its gloom.” The Caliph
answered, “Thou dost well to remind me, for that I had wholly
forgotten it; so fare forth and change thy vestments while I do the
same with mine.” Presently the twain donned habits of stranger
merchants and issued out by a private postern of the palace-garden,
which led them into the fields. After they had skirted the city, they
reached the Euphrates’ bank at some distance from the gate
opening on that side, without having observed aught of disorder;
then they crossed the river in the first ferry-boat they found, and,
making a second round on the further side, they passed over the
bridge that joined the two halves of Baghdad-town. At the bridge-
foot they met with a blind old man who asked alms of them; and the
Caliph turned about and crossed his palm with a dinar, whereupon
the beggar caught hold of his hand, and held him fast, saying, “O
beneficent man, whoso thou ever may be, whom Allah hath inspired
to bestow an alms upon me, refuse not the favour I crave of thee,
which is, to strike me a buffet upon the ear, for that I deserve such
punishment and a greater still.” After these words he quitted his hold
of the Caliph’s hand that it might smite him, yet for fear lest the
stranger pass on without so doing he grasped him fast by his long
robe.——And as the morn began to dawn Shahrazad held her peace
till
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookfinal.com