Author(s): Craig Grummitt
Edition: 1st Edition
Publisher: Manning Publications
Year: 2018
Language: English
Pages: 571
Tags: iOS, Swift
iOS Development with Swift......Page 1
brief contents......Page 7
contents......Page 9
What is Swift?......Page 17
Why learn Swift?......Page 18
acknowledgments......Page 20
about this book......Page 21
How this book is organized......Page 22
Book forum......Page 24
about the author......Page 25
about the cover illustration......Page 26
Part 1 Introducing Xcode and Swift......Page 27
1.1 Exploring iOS SDK......Page 29
1.2 Creating an Xcode project......Page 31
1.2.1 Templates......Page 32
1.2.2 Project options......Page 35
1.3 Exploring the Xcode interface......Page 36
1.3.1 Toolbar area......Page 37
1.3.3 Navigator area......Page 38
1.3.4 Editor area......Page 39
1.4.1 Storyboards and nibs......Page 41
1.4.3 Interface Builder......Page 42
1.4.4 Object Library......Page 43
1.4.5 Document Outline......Page 44
1.4.6 Inspectors......Page 45
1.5.2 Running your app in the simulator......Page 48
1.5.4 Simulator features......Page 49
1.6.2 Peeking at the completed app’s storyboard......Page 50
1.6.3 Tweaking the code......Page 51
1.7 Summary......Page 52
2 Introduction to Swift playgrounds......Page 55
2.1 Xcode playground......Page 56
2.1.1 Results sidebar......Page 57
2.1.3 Console......Page 58
2.2 Type safety and type inference......Page 59
2.2.1 Converting numeric types......Page 61
2.3.1 Arrays......Page 62
2.3.2 Sets......Page 63
2.4 Control Flow......Page 64
2.4.1 for-in......Page 65
2.4.2 switch statement......Page 66
2.5.1 Modifying external parameter names......Page 67
2.6 Optionals......Page 68
2.6.2 Unwrapping an optional......Page 70
2.6.3 Optional chaining......Page 74
2.7 Tuples......Page 75
2.7.2 Tuple magic......Page 76
2.8.1 map......Page 77
2.8.2 Closures......Page 78
2.8.4 reduce......Page 79
2.9 Summary......Page 80
3 Swift objects......Page 81
3.1.1 Defining a class......Page 82
3.1.2 Properties......Page 83
3.1.3 Initializers......Page 84
3.1.4 Methods......Page 85
3.1.5 Computed properties......Page 89
3.1.6 Class inheritance......Page 91
3.1.7 Protocols......Page 94
3.2 Structures......Page 97
3.2.1 Structures vs. classes......Page 98
3.3.1 Extensions of your type......Page 102
3.3.2 Extensions of their type......Page 103
3.3.3 Operator overloading......Page 104
3.3.4 Generics......Page 105
3.4 Summary......Page 106
Part 2 Building your interface......Page 107
4 View controllers, views, and outlets......Page 109
4.1 View hierarchy......Page 110
4.2 Model-view-controller......Page 111
4.3 View controller......Page 112
4.3.1 Creating a custom view controller......Page 113
4.3.2 Customizing a UIViewController subclass......Page 114
4.3.3 Initial view controller......Page 116
4.4.1 Managing views in code......Page 120
4.4.2 Managing views in Interface Builder......Page 123
4.5 Summary......Page 130
5 User interaction......Page 131
5.1.1 Buttons......Page 132
5.1.2 Text field......Page 135
5.1.3 Other controls......Page 140
5.2 Touching views......Page 141
5.2.2 Overriding touch methods......Page 142
5.2.3 The responder chain......Page 143
5.3 Gesture recognizers......Page 145
5.3.1 Pan gesture......Page 147
5.3.2 Pinch gesture......Page 151
5.3.4 Simultaneous gesture recognizers......Page 152
5.3.5 Tap gesture in code......Page 155
5.4 Summary......Page 157
6 Adaptive layout......Page 159
6.1 The problems......Page 160
6.2 Auto layout......Page 161
6.2.2 Auto layout in Interface Builder......Page 163
6.2.3 Auto layout in code......Page 179
6.3 Autoresizing......Page 183
6.3.1 Autoresizing in code......Page 184
6.3.3 Autoresizing considerations......Page 186
6.4.1 Receiving transition events......Page 187
6.4.2 Receiving layout events......Page 189
6.5 Choosing an approach......Page 191
6.6 Summary......Page 192
7.1 Size classes......Page 193
7.1.1 Size classes in code......Page 196
7.1.2 Size classes in Interface Builder......Page 202
7.2.1 The problem with auto layout......Page 211
7.2.2 Stack view properties......Page 213
7.2.3 Simple stack view in Interface Builder......Page 215
7.2.4 Nested stack views in Interface Builder......Page 217
7.2.5 Adding or removing views from a stack view......Page 220
7.2.6 Stack views in code......Page 221
7.3 Summary......Page 223
8 Keyboard notifications, animation, and scrolling......Page 225
8.1 The problem with the keyboard......Page 226
8.2 Dismissing the keyboard......Page 227
8.2.2 Detecting when to dismiss the keyboard......Page 228
8.3.1 What is a notification?......Page 231
8.3.2 Observing a keyboard frame change notification......Page 232
8.3.3 Unregistering a notification......Page 233
8.3.4 Extracting keyboard information from the notification......Page 234
8.3.5 Getting a reference to the first responder......Page 235
8.3.6 Calculating the offset to animate......Page 236
8.4 Animating views......Page 237
8.4.1 Animating the view from under the keyboard......Page 238
8.4.2 Diving deeper into animating views with a sample bar chart......Page 239
8.5.1 Scroll view with form content and keyboard......Page 244
8.5.2 Diving deeper into scroll views with image content......Page 248
8.6 Summary......Page 249
Part 3 Building your app......Page 251
9 Tables and navigation......Page 253
9.1 Displaying data in table views......Page 254
9.1.1 Setting up a table view controller in the storyboard......Page 255
9.1.2 Displaying data in the table view......Page 260
9.2 Adding a row......Page 268
9.2.1 Embedding a navigation controller......Page 269
9.2.2 Creating a segue......Page 271
9.2.3 Embedding second navigation controller......Page 274
9.2.4 Communicating with the books scene using your own delegate......Page 275
9.2.5 Adding data to the table......Page 278
9.3.1 Creating a segue from a row......Page 281
9.3.2 Passing in the book object to edit......Page 283
9.3.3 Removing the view controller......Page 284
9.4 Using large titles......Page 285
9.5 Deleting a row......Page 286
9.6 Summary......Page 288
10 Collections, searching, sorting, and tab bars......Page 289
10.1.1 Creating a sort method to sort the books array......Page 290
10.1.2 Changing sort order......Page 291
10.2 Searching the data......Page 296
10.2.1 Creating a search controller......Page 297
10.2.2 Adding the search controller to the view controller......Page 298
10.2.3 Filtering the data......Page 299
10.2.4 Removing and updating rows with filtered data......Page 301
10.3 Displaying data in collection views......Page 304
10.3.1 Creating custom collection cells......Page 306
10.3.2 Displaying data in a custom collection view cell......Page 307
10.3.4 Adding a search bar to the collection view......Page 309
10.3.5 Creating a second section......Page 311
10.3.6 Implementing the flow layout delegate......Page 312
10.4 Creating sections with a tab bar controller......Page 314
10.4.1 Sharing data between tabs......Page 316
10.5 Summary......Page 321
11 Local data persistence......Page 323
11.1.1 Preserving and restoring state......Page 324
11.1.2 Preserving user preferences on the device......Page 326
11.2.1 Storage setup......Page 328
11.2.2 Structured data files......Page 332
11.2.3 Archiving objects......Page 338
11.2.4 SQLite......Page 341
11.2.5 Core Data......Page 348
11.3 Summary......Page 361
12 Data persistence in iCloud......Page 363
12.1 Setting up your app for iCloud......Page 364
12.2 Persisting data with ubiquitous key-value store......Page 365
12.3 Storing data using CloudKit......Page 368
12.3.1 Updating the model for CloudKit......Page 370
12.3.2 Adding a book record to CloudKit......Page 372
12.3.3 Updating a book record in CloudKit......Page 378
12.3.4 Loading book records in CloudKit......Page 379
12.3.5 Deleting a book record in CloudKit......Page 381
12.3.6 Managing CloudKit errors......Page 382
12.3.7 Refreshing CloudKit data......Page 386
12.3.8 Subscribing to changes......Page 388
12.4 Summary......Page 395
13 Graphics and media......Page 397
13.1 Adding images to your app with an asset catalog......Page 398
13.1.1 Adding image sets......Page 399
13.1.2 Adding app icons......Page 403
13.2 Displaying a launch screen......Page 405
13.3 Drawing with Core Graphics......Page 407
13.3.2 Describing a path......Page 408
13.3.3 Drawing into the graphics context......Page 409
13.3.5 Drawing paths with UIBezierPath drawing methods......Page 410
13.3.6 Rendering views in Interface Builder......Page 411
13.3.7 Creating a star-rating view......Page 412
13.4 Drawing with Core Animation......Page 415
13.5 Using the camera......Page 417
13.5.1 Taking photos with the image picker controller......Page 418
13.5.2 Selecting photos from photo library with the image picker controller......Page 423
13.5.3 Taking photos with AVFoundation......Page 424
13.6 Playing sounds......Page 431
13.7 Summary......Page 433
14 Networking......Page 435
14.1 Using a web service......Page 436
14.2 Setting up a books service......Page 437
14.3 Communicating with the web service......Page 439
14.4.2 URLSession......Page 440
14.5 Setting up the URL request......Page 442
14.7 Examining the data......Page 444
14.8 Parsing JSON data with JSONSerialization......Page 446
14.9 Parsing JSON data with JSONDecoder......Page 447
14.10 Parsing JSON data with SwiftyJSON......Page 450
14.10.1 Integrating SwiftyJSON with Carthage......Page 452
14.10.2 Using SwiftyJSON......Page 455
14.11 Downloading data from a web service......Page 457
14.11.1 Accessing insecure domains......Page 459
14.13 Cancelling a task......Page 462
14.14 Summary......Page 463
15 Debugging and testing......Page 465
15.2 Debugging mode......Page 466
15.3 Debugging crash logs in the console......Page 469
15.3.1 Solving a crash caused by an outlet......Page 471
15.3.2 Solving a crash caused by an action......Page 473
15.4 Examining variables and breakpoints......Page 474
15.4.1 Examining a variable with print......Page 475
15.4.2 Pausing your app with a breakpoint......Page 476
15.4.4 Controlling the app’s execution using the debug bar......Page 478
15.4.5 Examining a variable with Quick Look......Page 480
15.4.6 Examining a variable with print description......Page 481
15.4.7 Examining a variable with LLDB......Page 482
15.4.8 Examining a variable with data tips......Page 483
15.4.10 Examining a variable in summary......Page 484
15.5.1 Debugging playback with debug gauges......Page 485
15.5.2 Debugging playback with instruments......Page 487
15.6 Debugging the user interface......Page 489
15.6.1 Debugging the user interface with the Debug View Hierarchy......Page 491
15.6.2 Debugging the user interface with runtime issues......Page 493
15.6.3 Solving the user interface problem......Page 494
15.7 Testing your app......Page 495
15.7.1 Testing for functionality......Page 496
15.7.2 Testing for performance......Page 499
15.7.3 Testing your user interface......Page 501
15.8 Summary......Page 504
Part 4 Finalizing your app......Page 505
16 Distributing your app......Page 507
16.1.1 Signing into Xcode......Page 508
16.1.2 Code signing your app......Page 509
16.2 Setting up an app in iTunes Connect......Page 516
16.3 Uploading your build to iTunes Connect......Page 519
16.4 Distributing your app to beta testers......Page 521
16.4.1 Distributing to beta testers manually......Page 522
16.4.2 Distributing to beta testers with TestFlight......Page 523
16.5 Distributing your app to the App Store......Page 531
16.6 Summary......Page 537
17.1 Further learning......Page 539
17.2 One more thing!......Page 541
appendix A Project settings......Page 543
A.1 General......Page 544
A.5 Build settings......Page 545
A.5.1 Changing your build settings for configurations......Page 546
A.5.2 Filtering build settings......Page 548
A.7 Build rules......Page 550
appendix B Swift syntax cheat sheets......Page 551
A......Page 555
B......Page 556
C......Page 557
D......Page 558
E......Page 559
H......Page 560
I......Page 561
M......Page 562
O......Page 563
R......Page 564
S......Page 565
T......Page 567
U......Page 568
X......Page 569
Z......Page 570
iOS Development with Swift-back......Page 571