Xcode Covers9, and SwiftiOS 11, 4 iOS 11 Programming Fundamentals with Swift SWIFT, XCODE, AND COCOA BASICS Matt Neuburg FOURTH EDITION iOS 11 Programming Fundamentals with Swift Swift, Xcode, and Cocoa Basics Matt Neuburg Boston iOS 11 Programming Fundamentals with Swift, Fourth Edition by Matt Neuburg Copyright © 2018 Matt Neuburg. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com/safari). For more information, contact our corporate/insti‐ tutional sales department: 800-998-9938 or [email protected]. Editor: Rachel Roumeliotis Cover Designer: Karen Montgomery Production Editor: Kristen Brown Interior Designer: David Futato Proofreader: O’Reilly Production Services Illustrator: Matt Neuburg Indexer: Matt Neuburg April 2015: First Edition October 2015: Second Edition October 2016: Third Edition October 2017: Fourth Edition Revision History for the Fourth Edition 2017-09-26: First release See http://oreilly.com/catalog/errata.csp?isbn=0636920107415 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. iOS 11 Programming Fundamentals with Swift, the image of a harp seal, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility 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. ISBN: 978-1-491-99931-8 [LSI] Table of Contents Preface. xiii Part I. Language 1. The Architecture of Swift. 3 Ground of Being 3 Everything Is an Object? 5 Three Flavors of Object Type 6 Variables 6 Functions 8 The Structure of a Swift File 9 Scope and Lifetime 11 Object Members 13 Namespaces 13 Modules 14 Instances 15 Why Instances? 17 The Keyword self 19 Privacy 20 Design 22 Object Types and APIs 23 Instance Creation, Scope, and Lifetime 25 Summary and Conclusion 26 2. Functions. 27 Function Parameters and Return Value 27 Void Return Type and Parameters 31 Function Signature 32 iii External Parameter Names 32 Overloading 34 Default Parameter Values 35 Variadic Parameters 35 Ignored Parameters 36 Modifiable Parameters 37 Function In Function 40 Recursion 42 Function As Value 42 Anonymous Functions 45 Define-and-Call 51 Closures 52 How Closures Improve Code 54 Function Returning Function 55 Closure Setting a Captured Variable 58 Closure Preserving Its Captured Environment 59 Escaping Closures 60 Curried Functions 61 Function References and Selectors 62 Function Reference Scope 64 Selectors 65 3. Variables and Simple Types. 69 Variable Scope and Lifetime 69 Variable Declaration 71 Computed Initializer 75 Computed Variables 75 Setter Observers 79 Lazy Initialization 80 Built-In Simple Types 82 Bool 82 Numbers 84 String 91 Character and String Index 96 Range 101 Tuple 103 Optional 106 4. Object Types. 119 Object Type Declarations and Features 119 Initializers 121 Properties 127 iv | Table of Contents Methods 130 Subscripts 132 Nested Object Types 135 Instance References 135 Enums 137 Raw Values 138 Associated Values 140 Enum Initializers 142 Enum Properties 143 Enum Methods 144 Why Enums? 145 Structs 146 Struct Initializers, Properties, and Methods 146 Struct As Namespace 148 Classes 148 Value Types and Reference Types 149 Subclass and Superclass 155 Class Initializers 160 Class Deinitializer 168 Class Properties and Methods 168 Polymorphism 170 Casting 174 Casting Down 175 Type Testing and Casting Down Safely 175 Type Testing and Casting Optionals 176 Bridging to Objective-C 177 Type Reference 178 Protocols 183 Why Protocols? 185 Protocol Type Testing and Casting 187 Declaring a Protocol 188 Protocol Composition 189 Optional Protocol Members 190 Class Protocol 192 Implicitly Required Initializers 192 Literal Convertibles 194 Generics 195 Generic Declarations 198 Type Constraints 200 Explicit Specialization 202 Associated Type Chains 204 Where Clauses 207 Table of Contents | v Extensions 211 Extending Object Types 211 Extending Protocols 213 Extending Generics 215 Umbrella Types 217 Any 217 AnyObject 219 AnyClass 221 Collection Types 222 Array 222 Dictionary 238 Set 245 5. Flow Control and More. 251 Flow Control 251 Branching 252 Loops 264 Jumping 268 Key Paths 284 Operators 286 Privacy 290 Private and Fileprivate 291 Public and Open 292 Privacy Rules 293 Introspection 293 Memory Management 294 Memory Management of Reference Types 295 Exclusive Access to Value Types 302 Part II. IDE 6. Anatomy of an Xcode Project. 307 New Project 307 The Project Window 309 The Navigator Pane 311 The Utilities Pane 316 The Editor 318 The Project File and Its Dependents 320 The Target 322 Build Phases 323 Build Settings 325 vi | Table of Contents Configurations 326 Schemes and Destinations 327 From Project to Running App 330 Build Settings 333 Property List Settings 333 Nib Files 334 Additional Resources 335 Code Files 338 Frameworks and SDKs 338 The App Launch Process 340 The Entry Point 341 UIApplicationMain 342 App Without a Storyboard 344 Renaming Parts of a Project 345 7. Nib Management. 347 The Nib Editor Interface 348 Document Outline 350 Canvas 352 Inspectors and Libraries 355 Nib Loading 356 When Nibs Are Loaded 356 Manual Nib Loading 358 Connections 360 Outlets 360 The Nib Owner 361 Automatically Configured Nibs 365 Misconfigured Outlets 366 Deleting an Outlet 368 More Ways to Create Outlets 368 Outlet Collections 372 Action Connections 372 More Ways to Create Actions 374 Misconfigured Actions 376 Connections Between Nibs — Not! 376 Additional Configuration of Nib-Based Instances 377 8. Documentation. 381 The Documentation Window 381 Class Documentation Pages 383 Quick Help 386 Symbol Declarations 388 Table of Contents | vii Header Files 389 Sample Code 390 Internet Resources 390 9. Life Cycle of a Project. 393 Runtime Environment 393 Backward Compatibility 394 Device Type 396 Version Control 397 Editing and Navigating Your Code 400 Autocompletion 401 Snippets 403 Structure Stubs 404 Fix-it and Live Syntax Checking 404 Navigation 405 Finding 407 Refactoring 408 Running in the Simulator 409 Debugging 410 Caveman Debugging 410 The Xcode Debugger 412 Testing 419 Clean 424 Running on a Device 425 Obtaining a Developer Program Membership 426 Signing an App 427 Automatic Signing 428 Obtaining a Development Certificate Manually 430 Obtaining a Development Provisioning Profile Manually 431 Running the App 434 Managing Development Certificates and Devices 434 Profiling 435 Gauges 435 Memory Debugging 436 Instruments 437 Localization 440 Distribution 445 Making an Archive 445 Obtaining a Distribution Certificate 446 Obtaining a Distribution Profile 447 Ad Hoc Distribution 448 Final App Preparations 450 viii | Table of Contents Screenshots and video previews 453 Property list settings 454 Submission to the App Store 455 Part III. Cocoa 10. Cocoa Classes. 461 Subclassing 461 Categories and Extensions 464 How Swift Uses Extensions 464 How You Use Extensions 465 How Cocoa Uses Categories 466 Protocols 467 Informal Protocols 469 Optional Methods 470 Some Foundation Classes 472 NSRange and NSNotFound 473 NSString and Friends 475 NSDate and Friends 478 NSNumber 480 NSValue 481 NSData 482 NSMeasurement and Friends 483 Equality and Comparison 484 NSArray and NSMutableArray 485 NSDictionary and NSMutableDictionary 487 NSSet and Friends 488 NSIndexSet 489 NSNull 489 Immutable and Mutable 490 Property Lists 491 Codable 492 Accessors, Properties, and Key–Value Coding 495 Swift Accessors 497 Key–Value Coding 498 Uses of Key–Value Coding 500 KVC and Outlets 501 Key Paths 502 Array Accessors 503 The Secret Life of NSObject 504 Table of Contents | ix 11. Cocoa Events. 507 Reasons for Events 508 Subclassing 508 Notifications 510 Receiving a Notification 511 Unregistering 513 Posting a Notification 514 Timer 515 Delegation 517 Cocoa Delegation 517 Implementing Delegation 519 Data Sources 521 Actions 522 The Responder Chain 525 Deferring Responsibility 526 Nil-Targeted Actions 526 Key–Value Observing 527 Swamped by Events 531 Delayed Performance 534 12. Memory Management. 537 Principles of Cocoa Memory Management 537 Rules of Cocoa Memory Management 538 What ARC Is and What It Does 539 How Cocoa Objects Manage Memory 540 Autorelease Pool 541 Memory Management of Instance Properties 543 Retain Cycles and Weak References 544 Unusual Memory Management Situations 546 Notification Observers 546 KVO Observers 548 Timers 548 Other Unusual Situations 550 Nib Loading and Memory Management 550 Memory Management of CFTypeRefs 551 Property Memory Management Policies 553 Debugging Memory Management Mistakes 555 13. Communication Between Objects. 557 Visibility by Instantiation 558 Visibility by Relationship 560 Global Visibility 561 x | Table of Contents Notifications and Key–Value Observing 563 Model–View–Controller 563 A. C, Objective-C, and Swift. 567 Index. 601 Table of Contents | xi Preface
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages645 Page
-
File Size-