ebook img

Learning Swift: Building Apps for macOS, iOS, and Beyond PDF

378 Pages·2018·16.18 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 Learning Swift: Building Apps for macOS, iOS, and Beyond

3 r C d ov E e d r i s ti S o wi n f t 4 + Learning S wift BUILDING APPS FOR macOS, iOS, AND BEYOND Jonathon Manning, Paris Buttfield-Addison & Tim Nugent THIRD EDITION Learning Swift Building Apps for macOS, iOS, and Beyond Jon Manning, Paris Buttfield-Addison, Tim Nugent BBeeiijjiinngg BBoossttoonn FFaarrnnhhaamm SSeebbaassttooppooll TTookkyyoo Learning Swift by Jonathon Manning, Paris Buttfield-Addison, and Tim Nugent Copyright © 2018 Secret Lab. 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 Indexer: Ellen Troutman-Zaig Production Editor: Melanie Yarbrough Interior Designer: David Futato Copyeditor: Rachel Head Cover Designer: Karen Montgomery Proofreader: Kim Cofer Illustrator: Rebecca Demarest March 2018: Third Edition Revision History for the Third Edition 2018-03-26: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781491987575 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Learning Swift, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors 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. 978-1-491-98757-5 [LSI] Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix Part I. Welcome to Swift 1. Getting Started. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Xcode 3 Getting Xcode 4 Creating Your First Project 4 Working with the Xcode Interface 6 The Editor 6 The Toolbar 8 The Navigator 10 Utilities 11 The Debug Area 13 Running Your Code 13 The iOS Simulator 14 Running on a Device 15 Distributing Your Apps 16 The Interface Builder 16 Interfaces in iOS 18 Conclusion 19 2. The Swift Programming Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 The Swift Language 22 Swift 3 Versus 4 24 Playgrounds and Swift 24 Comments 26 iii Imports 27 Variables 27 Operators 30 Collections 31 Arrays 31 Tuples 32 Dictionaries 33 Control Flow 34 Loops 35 Switches 36 Types 39 Working with Strings 39 Sets 42 Enumerations 44 Type Safety and Conversion 46 Optionals 48 Type Casting 51 Functions and Closures 53 Using Functions as Variables 56 Closures 57 Convenience Features 59 The defer Keyword 59 The guard Keyword 60 Making Your Code Swifty 60 Conclusion 61 3. Object-Oriented Development in Swift. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Classes and Objects 63 Initialization and Deinitialization 64 Properties 66 Inheritance 69 Protocols 70 Extensions 72 Access Control 75 Operator Overloading and Custom Operators 78 Subscripts 80 Generics 81 Structures 82 Error Handling 84 Memory Management 87 Design Patterns in Swift 91 Model–View–Controller 92 iv | Table of Contents Delegation 93 Swift Libraries 96 Structuring an App 97 Swift Package Manager 101 Conclusion 107 Part II. Building Selfiegram 4. Setting Up Our App. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Designing Selfiegram 112 Creating the Project 115 The Structure 118 Renaming the Views 120 5. Building the Model Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 The Selfie Object 123 The SelfieStore 127 Testing the SelfieStore 130 Filling Out the Method Stubs 136 6. Building the Selfie List UI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Creating the Selfie List 145 Improving the Selfie List 151 7. Adding and Deleting Selfies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Deleting Selfies 155 Taking New Selfies 157 8. Viewing and Editing Selfies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 The Selfie Viewer 165 Connecting the Code and the UI 167 Editing a Selfie 172 9. Adding Location Info to the Selfies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Updating the Model 175 Testing Our New Model 178 Showing a Selfie’s Location 179 Expanding the Map 183 Determining Locations 186 Configuring the App to Support Location 186 Talking to the Location Hardware 188 Table of Contents | v 10. Building a Settings View. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Building the Settings UI 193 Connecting the Settings into Our Hierarchy 195 Hooking Up the Settings 196 Implementing the Settings 197 11. Reminders and Notifications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Adding a Reminder to the Settings 201 Creating a Notification 203 Part III. Polishing Selfiegram 12. Theming and Sharing Selfiegram. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Custom Fonts 214 Making the Theme 217 Sharing Selfies 221 Sharing from the Detail View Controller 222 Sharing from the List View Controller 224 13. Custom Views and View Controllers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 A Camera View 228 The Camera View Controller 231 Building the UI 231 Connecting the UI 234 Talking to the Camera 234 Calling the Capture View Controller 240 14. Image Overlays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Creating the Overlay Model 245 Testing the Overlay Manager 252 Writing the Tests 252 Returning Available Overlays 255 Downloading Overlay Information 256 Downloading Overlay Images 258 15. Overlay UI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 Building the UI 263 Hooking Up the UI 266 Creating the Overlay View 266 Presenting the Overlays 268 Initial Setup 268 vi | Table of Contents Drawing Eyebrows 271 Vision and Image Detection 274 Connecting It into the App 279 16. Localization and Internationalization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Internationalization 284 Generating the String Table 286 Localization 287 Translating the Strings 290 Testing Locales 290 Pseudolanguages 292 Previewing Localizations 294 Conclusion 296 Part IV. Beyond Developing Selfiegram 17. Debugging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 The Debugger 299 Breakpoints 300 Inspecting Code 304 Controlling Program Flow 307 Performance 309 Instruments 310 18. UI Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 UI Testing Classes 313 Writing UI Tests 314 A Basic Test 314 Recording UI Actions 315 Checking if Elements Exist 318 Handling UI Interruptions 319 19. Automating Chores with Fastlane. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 The Fastlane Tools 322 Installing Fastlane 323 Installing via Homebrew 324 Installing via RubyGems 324 Installing via Direct Download 324 Setting Up a Project 324 Doing a Build 325 Configuring a Fastfile 327 Table of Contents | vii 20. Using Fastlane’s Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 Code Signing with match 329 The Problem That Match Solves 329 match’s Solution 332 Generating Screenshots Using snapshot 333 Using Boarding to Add Testers to TestFlight 337 Managing Your App’s Presence on the App Store Using deliver 339 Getting the Metadata 340 Submitting New Metadata 340 Looking Further 341 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 viii | Table of Contents

Description:
Get valuable hands-on experience with Swift, the open source programming language developed by Apple. With this practical guide, skilled programmers with little or no knowledge of Apple development will learn how to code with the latest version of Swift by developing a working iOS app from start to
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.