IOS 12 programming fundamentals with swift: swift, xcode, and cocoa basics [Fifth edition] 9781492044550, 3973983984, 1492044555, 9781492044505, 1492044504, 9781492044529, 1492044520

Move into iOS development by getting a firm grasp of its fundamentals, including the Xcode 10 IDE, Cocoa Touch, and the

593 122 4MB

English Pages 652 Year 2018

Report DMCA / Copyright

DOWNLOAD FILE

IOS 12 programming fundamentals with swift: swift, xcode, and cocoa basics [Fifth edition]
 9781492044550, 3973983984, 1492044555, 9781492044505, 1492044504, 9781492044529, 1492044520

Table of contents :
Copyright......Page 4
Table of Contents......Page 5
Preface......Page 15
The Scope of This Book......Page 17
Versions......Page 18
Acknowledgments......Page 19
From the Programming iOS 4 Preface......Page 20
Conventions Used in This Book......Page 21
Safari® Books Online......Page 22
How to Contact Us......Page 23
Part I. Language......Page 25
Ground of Being......Page 27
Everything Is an Object?......Page 29
Variables......Page 30
Functions......Page 32
The Structure of a Swift File......Page 33
Scope and Lifetime......Page 35
Namespaces......Page 37
Modules......Page 38
Instances......Page 39
Why Instances?......Page 41
The Keyword self......Page 43
Privacy......Page 44
Design......Page 46
Object Types and APIs......Page 47
Summary and Conclusion......Page 49
Function Parameters and Return Value......Page 51
Void Return Type and Parameters......Page 55
External Parameter Names......Page 56
Overloading......Page 58
Variadic Parameters......Page 59
Ignored Parameters......Page 60
Modifiable Parameters......Page 61
Function in Function......Page 64
Function As Value......Page 66
Anonymous Functions......Page 69
Define-and-Call......Page 75
Closures......Page 76
How Closures Improve Code......Page 78
Function Returning Function......Page 79
Closure Setting a Captured Variable......Page 82
Closure Preserving Its Captured Environment......Page 83
Curried Functions......Page 84
Function References and Selectors......Page 86
Function Reference Scope......Page 88
Selectors......Page 89
Variable Scope and Lifetime......Page 93
Variable Declaration......Page 95
Computed Initializer......Page 98
Computed Variables......Page 100
Setter Observers......Page 103
Lazy Initialization......Page 104
Bool......Page 106
Numbers......Page 108
String......Page 116
Character and String Index......Page 120
Range......Page 125
Tuple......Page 127
Optional......Page 129
Object Type Declarations and Features......Page 143
Initializers......Page 145
Properties......Page 151
Methods......Page 154
Subscripts......Page 156
Instance References......Page 159
Enums......Page 161
Raw Values......Page 162
Associated Values......Page 164
Enum Case Iteration......Page 166
Enum Initializers......Page 167
Enum Properties......Page 168
Enum Methods......Page 169
Why Enums?......Page 170
Struct Initializers, Properties, and Methods......Page 171
Classes......Page 173
Value Types and Reference Types......Page 174
Subclass and Superclass......Page 180
Class Initializers......Page 185
Class Deinitializer......Page 193
Class Properties and Methods......Page 194
Polymorphism......Page 196
Casting......Page 199
Type Testing and Casting Down Safely......Page 200
Bridging to Objective-C......Page 202
From Instance to Type......Page 204
Type as Value......Page 205
The Keyword Self......Page 207
Comparing Types......Page 208
Summary of Type Terminology......Page 209
Protocols......Page 210
Why Protocols?......Page 211
Protocol Type Testing and Casting......Page 213
Declaring a Protocol......Page 214
Protocol Composition......Page 215
Optional Protocol Members......Page 216
Class Protocol......Page 218
Implicitly Required Initializers......Page 219
Literal Convertibles......Page 220
Generics......Page 221
Generic Declarations......Page 224
Contradictory Resolution......Page 226
Type Constraints......Page 227
Explicit Specialization......Page 229
Generic Invariance......Page 231
Associated Type Chains......Page 232
Where Clauses......Page 234
Extending Object Types......Page 237
Extending Protocols......Page 239
Extending Generics......Page 241
Any......Page 243
AnyObject......Page 245
AnyClass......Page 247
Array......Page 248
Dictionary......Page 264
Set......Page 271
Flow Control......Page 277
Branching......Page 278
Loops......Page 290
Jumping......Page 295
Privacy......Page 310
Private and Fileprivate......Page 312
Public and Open......Page 313
Introspection......Page 314
Operators......Page 315
Synthesized Protocol Implementations......Page 319
Key Paths......Page 322
Dynamic Member Lookup......Page 324
Memory Management of Reference Types......Page 325
Exclusive Access to Value Types......Page 332
Part II. IDE......Page 335
New Project......Page 337
The Project Window......Page 339
The Navigator Pane......Page 341
The Utilities Pane......Page 346
The Editor......Page 347
The Project File and Its Dependents......Page 349
What’s In the Project Folder......Page 350
Groups......Page 351
Build Phases......Page 352
Build Settings......Page 354
Configurations......Page 355
Schemes and Destinations......Page 357
From Project to Built App......Page 359
Property List Settings......Page 362
Nib Files......Page 363
Additional Resources......Page 364
Code Files......Page 366
Frameworks and SDKs......Page 367
The Entry Point......Page 369
UIApplicationMain......Page 370
App Without a Storyboard......Page 372
Renaming Parts of a Project......Page 373
Chapter 7. Nib Management......Page 375
The Nib Editor Interface......Page 376
Document Outline......Page 377
Canvas......Page 380
Inspectors and Libraries......Page 382
Nib Loading......Page 383
When Nibs Are Loaded......Page 384
Manual Nib Loading......Page 385
Outlets......Page 387
The Nib Owner......Page 388
Automatically Configured Nibs......Page 392
Misconfigured Outlets......Page 393
Deleting an Outlet......Page 395
More Ways to Create Outlets......Page 396
Outlet Collections......Page 398
Action Connections......Page 399
More Ways to Create Actions......Page 401
Connections Between Nibs — Not!......Page 403
Additional Configuration of Nib-Based Instances......Page 404
The Documentation Window......Page 409
Class Documentation Pages......Page 410
Quick Help......Page 414
Symbol Declarations......Page 416
Header Files......Page 417
Internet Resources......Page 418
Environmental Dependencies......Page 421
Backward Compatibility......Page 422
Device Type......Page 424
Arguments and Environment Variables......Page 425
Conditional Compilation......Page 426
Version Control......Page 428
Editing and Navigating Your Code......Page 431
Autocompletion......Page 433
Snippets......Page 434
Fix-it and Live Syntax Checking......Page 436
Navigation......Page 437
Finding......Page 439
Running in the Simulator......Page 440
Caveman Debugging......Page 441
The Xcode Debugger......Page 444
Testing......Page 451
Unit Tests......Page 452
Interface Tests......Page 455
Running on a Device......Page 457
Obtaining a Developer Program Membership......Page 458
Signing an App......Page 459
Automatic Signing......Page 460
Manual Signing......Page 463
Running the App......Page 465
Gauges......Page 466
Memory Debugging......Page 468
Instruments......Page 469
Localization......Page 472
Making an Archive......Page 477
The Distribution Certificate......Page 478
The Distribution Profile......Page 480
Distribution for Testing......Page 481
Final App Preparations......Page 482
Screenshots and Video Previews......Page 485
Property List Settings......Page 487
Submission to the App Store......Page 488
Part III. Cocoa......Page 491
Subclassing......Page 493
How Swift Uses Extensions......Page 496
How Cocoa Uses Categories......Page 497
Protocols......Page 499
Optional Methods......Page 501
Some Foundation Classes......Page 504
NSRange and NSNotFound......Page 505
NSString and Friends......Page 507
NSDate and Friends......Page 510
NSNumber......Page 511
NSValue......Page 513
NSData......Page 514
Equality, Hashability, and Comparison......Page 515
NSArray and NSMutableArray......Page 518
NSSet and Friends......Page 520
NSIndexSet......Page 521
Immutable and Mutable......Page 522
Property Lists......Page 524
Codable......Page 525
Accessors, Properties, and Key–Value Coding......Page 528
Swift Accessors......Page 529
Key–Value Coding......Page 531
Uses of Key–Value Coding......Page 532
Cocoa Key Paths......Page 534
The Secret Life of NSObject......Page 535
Reasons for Events......Page 537
Subclassing......Page 538
Notifications......Page 540
Receiving a Notification......Page 541
Unregistering......Page 543
Posting a Notification......Page 544
Timer......Page 545
Cocoa Delegation......Page 547
Implementing Delegation......Page 549
Actions......Page 551
The Responder Chain......Page 554
Deferring Responsibility......Page 555
Nil-Targeted Actions......Page 556
Key–Value Observing......Page 557
Registration and Notification......Page 558
Unregistering......Page 559
Key–Value Observing Example......Page 560
Swamped by Events......Page 561
Delayed Performance......Page 564
Principles of Cocoa Memory Management......Page 567
Rules of Cocoa Memory Management......Page 568
What ARC Is and What It Does......Page 569
How Cocoa Objects Manage Memory......Page 570
Autorelease Pool......Page 571
Memory Management of Instance Properties......Page 573
Retain Cycles and Weak References......Page 574
Notification Observers......Page 576
Timers......Page 578
Nib Loading and Memory Management......Page 580
Memory Management of CFTypeRefs......Page 581
Property Memory Management Policies......Page 583
Debugging Memory Management Mistakes......Page 585
Chapter 13. Communication Between Objects......Page 587
Visibility by Instantiation......Page 588
Visibility by Relationship......Page 590
Global Visibility......Page 591
Model–View–Controller......Page 593
The C Language......Page 597
C Data Types......Page 598
C Enums......Page 599
C Structs......Page 602
C Pointers......Page 603
C Arrays......Page 605
C Functions......Page 607
Objective-C Objects and C Pointers......Page 609
Objective-C Objects and Swift Objects......Page 610
Exposure of Swift to Objective-C......Page 611
Bridged Types and Boxed Types......Page 612
Objective-C Methods......Page 615
Objective-C Initializers and Factories......Page 618
Selectors......Page 619
CFTypeRefs......Page 621
Blocks......Page 623
API Markup......Page 625
Bilingual Targets......Page 627
Index......Page 631
About the Author......Page 651

Polecaj historie