ebook img

iOS 11 Programming Fundamentals with Swift: Swift, Xcode, and Cocoa Basics PDF

645 Pages·2017·6.301 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview iOS 11 Programming Fundamentals with Swift: Swift, Xcode, and Cocoa Basics

X c C o o d v e er 9, s i a O n S d 1 S 1, w i f t 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

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.