
Beginning iPhone
Development
with Swift 4
Exploring the iOS SDK
—
Fourth Edition
—
Molly K. Maskrey

Beginning iPhone
Development with
Swift 4
Exploring the iOS SDK
Fourth Edition
Molly K. Maskrey

Beginning iPhone Development with Swift 4: Exploring the iOS SDK
Molly K. Maskrey
Parker, Colorado, USA
ISBN-13 (pbk): 978-1-4842-3071-8 ISBN-13 (electronic): 978-1-4842-3072-5
https://doi.org/10.1007/978-1-4842-3072-5
Library of Congress Control Number: 2017957132
Copyright © 2017 by Molly K. Maskrey
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material
is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting,
reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval,
electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter
developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly
analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for
exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under
the provisions of the Copyright Law of the Publisher's location, in its current version, and permission for use must
always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright
Clearance Center. Violations are liable to prosecution under the respective Copyright Law.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material
contained herein.
Managing Director: Welmoed Spahr
Editorial Director: Todd Green
Acquisitions Editor: Aaron Black
Development Editor: James Markham
Technical Reviewer: Bruce Wade
Coordinating Editor: Jessica Vakili
Copy Editor: Kim Wimpsett
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM
Finance Inc is a Delaware corporation.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional
use. eBook versions and licenses are also available for most titles. For more information, reference our
Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.
Any source code or other supplementary materials referenced by the author in this text is available to
readers at www.apress.com. For detailed information about how to locate your book’s source code, go to
www.apress.com/source-code/.
Printed on acid-free paper

Another year, another revision of this book, as well as one of my own personal tribe.
Still around after more than two years of ups and downs, KP stuck beside me during my
most dicult times. No matter what I did, her sanity and silliness kept me going. She was,
for a long while, my muse…my inspiration to write and to keep it as fun as possible…never
taking myself too seriously. Now, as our paths slightly diverge, the qualities she’s left permeate
my days and make me a better self.
Mellie has been my rock. We’ve been in and out of being friends, and I hope she knows
I consider her the closest friend I’ve ever had. I probably wouldn’t be doing this if it weren’t
for the love she’s shown me.
Tonya came into my life recently and has been such a godsend to help me become more
condent in all I say and do, and I can’t thank her enough.
She probably has no clue she’s in here, but Lauren kept me going for the past year.
When I would’ve preferred to lay in the dark and sleep and zone out, without any
aggressiveness Lauren would give me something in her words to keep to going…to
not give up. She—I won’t say forced, but she was very persuasive—got me into a
program that is, this very day, putting me onto a better path for myself and my future and
the future of anyone alongside me for my journey.
Finally, to Jennifer…our lives have radically changed over the past year. But, as partners
in business and advocates for each other’s happy life, our relationship bond, while admittedly
very tumultuous, has solidied into something we both see as pretty damn good.
In my personal struggles over the preceding year, these friends kept me from falling into
an abyss so deep I might never have returned. Writing can be a lonely thing, and having
a support system such as these beautiful, wonderful women are the only reason this endeavor
was a success. A special friend told me last year that some friends are only in your life for
a season. I pray that these women are friends for a lifetime.
—MM, August 2017

v
Contents at a Glance
About the Author ���������������������������������������������������������������������������������������������������xvii
About the Technical Reviewer ��������������������������������������������������������������������������������xix
Acknowledgments ��������������������������������������������������������������������������������������������������xxi
■Chapter 1: Getting to Know the iOS Landscape ����������������������������������������������������� 1
■Chapter 2: Writing Your First App ������������������������������������������������������������������������ 13
■Chapter 3: Basic User Interactions ���������������������������������������������������������������������� 51
■Chapter 4: Adding Intermediate-Level User Interactions ������������������������������������ 87
■Chapter 5: Working with Device Rotations �������������������������������������������������������� 131
■Chapter 6: Creating a Multiview Application ����������������������������������������������������� 179
■Chapter 7: Using Tab Bars and Pickers ������������������������������������������������������������� 209
■Chapter 8: Introducing Table Views ������������������������������������������������������������������� 255
■Chapter 9: Adding Navigation Controllers to Table Views ��������������������������������� 309
■Chapter 10: Collection Views ����������������������������������������������������������������������������� 343
■Chapter 11: Split Views and Popovers for iPad Apps ���������������������������������������� 357
■Chapter 12: App Customization with Settings and Defaults ������������������������������ 383
■Chapter 13: Persistence: Saving Data Between App Launches ������������������������� 419
■Chapter 14: Graphics and Drawing �������������������������������������������������������������������� 465
■Appendix A: An Introduction to Swift ���������������������������������������������������������������� 491
Index ��������������������������������������������������������������������������������������������������������������������� 547