Table Of ContentCovers Swift 4, Xcode 9, and iOS 11
Craig Grummitt
M A N N I N G
Devices, orientation, and multitasking modes
for iOS size classes
Horizontal (width)
Compact Regular
iPhone 4S, 5, 6, 7, 8, iPhone 6 Plus, 7 Plus,
SE landscape 8 Plus landscape
ct
a
p
m
o
C
iPhone portrait iPad portrait/landscape
ht)
g
ei
h
al (
c
erti iPad landscape: iPad landscape:
V split-view secondary app/ split-view primary app
slide-over
ar
ul
g
e
R
iPad portrait: iPad landscape:
split-view/slide-over side-by-side
iOS Development with Swift
ii
iOS Development
with Swift
CRAIG GRUMMITT
MANNING
SHELTER ISLAND
For online information and ordering of this and other Manning books, please visit
www.manning.com. The publisher offers discounts on this book when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
20 Baldwin Road
PO Box 761
Shelter Island, NY 11964
Email: orders@manning.com
©2018 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in
any form or by means electronic, mechanical, photocopying, or otherwise, without prior written
permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in the book, and Manning
Publications was aware of a trademark claim, the designations have been printed in initial caps
or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have
the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Recognizing also our responsibility to conserve the resources of our planet, Manning books
are printed on paper that is at least 15 percent recycled and processed without the use of
elemental chlorine.
Manning Publications Co. Development editor: Helen Stergius
20 Baldwin Road Review editor: Aleksandar Dragosavljevic´
PO Box 761 Technical development editor: Doug Sparling
Shelter Island, NY 11964 Project editor: Kevin Sullivan
Copyeditor: Katie Petito
Proofreader: Katie Tennant
Technical proofreader: Doug Warren
Typesetter and cover design: Marija Tudor
ISBN 9781617294075
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – EBM – 22 21 20 19 18 17
brief contents
PART 1 INTRODUCING XCODE AND SWIFT ................................... 1
1 ■ Your first iOS application 3
2 ■ Introduction to Swift playgrounds 29
3 ■ Swift objects 55
PART 2 BUILDING YOUR INTERFACE .......................................... 81
4 ■ View controllers, views, and outlets 83
5 ■ User interaction 105
6 ■ Adaptive layout 133
7 ■ More adaptive layout 167
8 ■ Keyboard notifications, animation, and scrolling 199
PART 3 BUILDING YOUR APP ................................................... 225
9 ■ Tables and navigation 227
10 ■ Collections, searching, sorting, and tab bars 263
11 ■ Local data persistence 297
12 ■ Data persistence in iCloud 337
13 ■ Graphics and media 371
14 ■ Networking 409
15 ■ Debugging and testing 439
v
vi BRIEF CONTENTS
PART 4 FINALIZING YOUR APP ................................................. 479
16 ■ Distributing your app 481
17 ■ What’s next? 513
contents
preface xv
acknowledgments xviii
about this book xix
about the author xxiii
about the cover illustration xxiv
PART 1 INTRODUCING XCODE AND SWIFT .................... 1
1 Your first iOS application 3
1.1 Exploring iOS SDK 3
1.2 Creating an Xcode project 5
Templates 6 ■ Project options 9
1.3 Exploring the Xcode interface 10
Toolbar area 11 ■ Utility area 12 ■ Navigator area 12
Editor area 13 ■ Debug area 15
1.4 Editing your app’s interface 15
Storyboards and nibs 15 ■ View controllers and views 16
Interface Builder 16 ■ Object Library 17 ■ Document
Outline 18 ■ Inspectors 19
1.5 Running your app 22
Running your app on a device 22 ■ Running your app in the
simulator 22 ■ Running your app 23 ■ Simulator features 23
vii
viii CONTENTS
1.6 Peeking at a completed app 24
Checking out a repository in Xcode 24 ■ Peeking at the
completed app’s storyboard 24 ■ Tweaking the code 25
1.7 Summary 26
2 Introduction to Swift playgrounds 29
2.1 Xcode playground 30
Results sidebar 31 ■ Automatic compiling 32
Console 32
2.2 Type safety and type inference 33
Converting numeric types 35 ■ Concatenating
strings 36
2.3 Collections 36
Arrays 36 ■ Sets 37 ■ Dictionaries 38
2.4 Control Flow 38
for-in 39 ■ switch statement 40
2.5 Functions 41
Modifying external parameter names 41 ■ Omitting
external parameter names 42 ■ Default parameter names 42
2.6 Optionals 42
Declaring an optional 44 ■ Unwrapping an optional 44
Optional chaining 48 ■ Final comments on optionals 49
2.7 Tuples 49
Tuples as return values 50 ■ Tuple magic 50
2.8 Higher-order functions 51
map 51 ■ Closures 52 ■ filter 53 ■ reduce 53
sorted 54
2.9 Summary 54
3 Swift objects 55
3.1 Classes 56
Defining a class 56 ■ Properties 57 ■ Initializers 58
Methods 59 ■ Computed properties 63 ■ Class inheritance 65
Protocols 68
3.2 Structures 71
Structures vs. classes 72
Description:iOS Development with Swift is a hands-on guide to creating apps for iPhone and iPad using the Swift language. Inside, you'll be guided through every step of the process for building an app, from first idea to App Store. This book fully covers Swift 4, Xcode 9, and iOS 1. Our video course, iOS Develo