Textbook Learning Swift Building Apps For Osx Ios and Beyond Jon Manning Ebook All Chapter PDF
Textbook Learning Swift Building Apps For Osx Ios and Beyond Jon Manning Ebook All Chapter PDF
https://textbookfull.com/product/learning-swift-building-apps-
for-macos-ios-and-beyond-2nd-edition-paris-buttfield-addison/
https://textbookfull.com/product/biota-grow-2c-gather-2c-cook-
loucas/
https://textbookfull.com/product/ios-11-swift-programming-
cookbook-solutions-and-examples-for-ios-apps-1st-edition-vandad-
nahavandipoor/
https://textbookfull.com/product/swift-4-for-absolute-beginners-
develop-apps-for-ios-4th-edition-stefan-kaczmarek/
Head First Swift 1st Edition Gray Anthony Manning Jon
Buttfield Addison Paris
https://textbookfull.com/product/head-first-swift-1st-edition-
gray-anthony-manning-jon-buttfield-addison-paris/
https://textbookfull.com/product/developing-inclusive-mobile-
apps-building-accessible-apps-for-ios-and-android-1st-edition-
rob-whitaker/
https://textbookfull.com/product/ios-10-sdk-development-creating-
iphone-and-ipad-apps-with-swift-chris-adamson/
https://textbookfull.com/product/ios-unit-testing-by-example-
xctest-tips-and-techniques-using-swift-1st-edition-jon-reid/
https://textbookfull.com/product/learn-swift-by-building-
applications-explore-swift-programming-through-ios-app-
development-1st-edition-emil-atanasov/
Learning Swift 3
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
iii
Working with Strings 40
Comparing Strings 41
Searching Strings 42
Optional Types 42
Type Casting 44
Tuples 45
Arrays 46
Dictionaries 48
Enumerations 48
Sets 50
Functions and Closures 51
Using Functions as Variables 54
Closures 56
The defer Keyword 58
The guard Keyword 58
Making your code Swifty 59
Conclusion 59
iv | Table of Contents
The Application Delegate 89
Window Controllers and View Controllers 89
Nibs and Storyboards 90
Conclusion 90
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Table of Contents | v
Conclusion 217
vi | Table of Contents
11. Images and Deletion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Adding Attachments 327
Adding Image Attachments 329
Viewing Attachments 335
Deleting Attachments 346
Conclusion 353
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.
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.
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.
Preface | xi
This element signifies a general note.
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:
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.
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 [email protected] and find us
on Twitter @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: 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.
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.
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.
• 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.
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.
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.
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).
If you don’t have a domain name, enter anything you like, as long as it looks
like a backward domain name (e.g., com.mycompany will work).
If you’re writing an application for the Mac App Store, you’ll also be prompted
for the App Store category (whether it’s a game, an educational app, a social net‐
working app, or something else).
Depending on the template, you may also be asked for other information (e.g.,
the file extension for your documents if you are creating a document-aware
application, such as a Mac app). You’ll also be asked which language you want to
use; because this book is about Swift, you should probably choose Swift! The
additional information needed for this project is covered in the following steps.
5. Make the application run on the iPhone by choosing iPhone from the Devices
drop-down list.
6. Leave the rest of the settings as shown in Figure 1-4. Click Next to create the
project.
7. Choose where to save the project. Select a location that suits you. We recommend
putting all your work related to this book (and other Swift programming learning
you might do) in one folder. You might notice a little checkbox for Source Con‐
trol; this creates a source code control repository for your code, giving you a
place where you can save and manage different versions of your code as you cre‐
ate them. While in general this is a good idea to use, for this example project,
make sure this is unchecked.
Once you’ve done this, Xcode will open the project, and you can now start using the
entire Xcode interface, as shown in Figure 1-5.
The editor
The Xcode editor (Figure 1-6) is where you’ll be spending most of your time. All
source code editing, interface design, and project configuration take place in this sec‐
tion of the application, which changes depending on which file you have open.
If you’re editing source code, the editor is a text editor, with code completion, syntax
highlighting, and all the usual features that developers have come to expect from an
integrated development environment. If you’re modifying a user interface, the editor
becomes a visual editor, allowing you to drag around the components of your inter‐
face. Other kinds of files have their own specialized editors as well.
When you first create a project, the editor will start by showing the project settings, as
seen in Figure 1-6.
The editor can also be split into a main editor and an assistant editor through the edi‐
tor selector. The assistant shows files that are related to the file open in the main edi‐
tor. It will continue to show files that have a relationship to whatever is open, even if
you open different files.
For example, if you open an interface file and then open the assistant, the assistant
will, by default, show related code for the interface you’re editing. If you open another
interface file, the assistant will show the code for the newly opened files.
At the top of the editor, you’ll find the jump bar. The jump bar lets you quickly jump
from the content that you’re editing to another piece of related content, such as a file
in the same folder. The jump bar is a fast way to navigate your project.
The toolbar
The Xcode toolbar (Figure 1-7) acts as mission control for the entire interface. It’s the
only part of Xcode that doesn’t significantly change as you develop your applications,
and it serves as the place where you can control what your code is doing.
From left to right, after the OS X window controls, the toolbar features the following
items:
Run button (Figure 1-8)
Clicking this button instructs Xcode to compile and run the application.
Sold,
Salt Beef and Pork, 40 Shillings for a
Cask of 2 Cwt.
Bisket, 17s. per hundred
Candles, 6½ per lb. &c.
Exchange 30 per Cent. or more.
I have heard that the Custom-house Books had one year 35000
Hogsheads of Sugar entred, which at 10l. per Hogshead, amounts to
350000l. Every Acre was supposed 10s. a year Profit to the national
Stock of England, besides what the Planter got, and Mouths fed by
it; but I must observe, the Crops of late years have very much failed,
and put many of them under great Necessities. The Soil fertile in the
Age past, seems now growing old, and past its teeming-time; they
endeavour to mend this by a few Cattle kept for the sake of Manure;
few, I say, because Land imploy’d this way, gives not 1/10 its Value.
Wherefore when a thoughtless Man has joined to unlucky Events
and Seasons an inadvertent way of living, he falls a Prey to the more
astronomical Heads of Factors, who supply him with Food and
Necessaries. The Hardships of many Planters at this time, through
such Inclemencies, cannot be better laid open to the Reader, than in
transcribing part of a Sermon, that I am informed was preached by
Command of his Excellency the Governor, May 1734.
A Charity S e r m o n at Bridgetown,
for the two Parishes, St. Philip, and Christ-Church.