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 finish. You’ll begin with Swift programming basics—including guidelines for making your code "Swifty"—and learn how to work with Xcode and its built-in Interface Builder. Then you’ll dive step-by-step into building and customizing a basic app for taking, editing, and deleting selfies. You’ll also tune and test the app for performance and manage the app’s presence in the App Store. Divided into four parts, this book includes: * **Swift 4 basics:** Learn Swift’s basic building blocks and the features of object-oriented development * **Building the Selfiegram app:** Build model objects and the UI for your selfie app and add location support, user settings, and notifications * **Polishing Selfiegram:** Create a theme and support for sharing and add custom views, image overlays, and localization * **Beyond app development:** Debug and performance test with Xcode, automate chores with Fastlane, and user-test the app with TestFlight Cover 1 Copyright 4 Table of Contents 5 Preface 11 Resources Used in This Book 11 Audience and Approach 11 Organization of This Book 12 Conventions Used in This Book 12 Using Code Examples 13 O’Reilly Safari 14 How to Contact Us 14 Acknowledgments 15 Part I. Welcome to Swift 17 Chapter 1. Getting Started 19 Xcode 19 Getting Xcode 20 Creating Your First Project 20 Working with the Xcode Interface 22 The Editor 22 The Toolbar 24 The Navigator 26 Utilities 27 The Debug Area 29 Running Your Code 29 The iOS Simulator 30 Running on a Device 31 Distributing Your Apps 32 The Interface Builder 32 Interfaces in iOS 34 Conclusion 35 Chapter 2. The Swift Programming Language 37 The Swift Language 38 Swift 3 Versus 4 40 Playgrounds and Swift 40 Comments 42 Imports 43 Variables 43 Operators 46 Collections 47 Arrays 47 Tuples 48 Dictionaries 49 Control Flow 50 Loops 51 Switches 52 Types 55 Working with Strings 55 Sets 58 Enumerations 60 Type Safety and Conversion 62 Optionals 64 Type Casting 67 Functions and Closures 69 Using Functions as Variables 72 Closures 73 Convenience Features 75 The defer Keyword 75 The guard Keyword 76 Making Your Code Swifty 76 Conclusion 77 Chapter 3. Object-Oriented Development in Swift 79 Classes and Objects 79 Initialization and Deinitialization 80 Properties 82 Inheritance 85 Protocols 86 Extensions 88 Access Control 91 Operator Overloading and Custom Operators 94 Subscripts 96 Generics 97 Structures 98 Error Handling 100 Memory Management 103 Design Patterns in Swift 107 Model–View–Controller 108 Delegation 109 Swift Libraries 112 Structuring an App 113 Swift Package Manager 117 Conclusion 123 Part II. Building Selfiegram 125 Chapter 4. Setting Up Our App 127 Designing Selfiegram 128 Creating the Project 131 The Structure 134 Renaming the Views 136 Chapter 5. Building the Model Object 139 The Selfie Object 139 The SelfieStore 143 Testing the SelfieStore 146 Filling Out the Method Stubs 152 Chapter 6. Building the Selfie List UI 161 Creating the Selfie List 161 Improving the Selfie List 167 Chapter 7. Adding and Deleting Selfies 171 Deleting Selfies 171 Taking New Selfies 173 Chapter 8. Viewing and Editing Selfies 181 The Selfie Viewer 181 Connecting the Code and the UI 183 Editing a Selfie 188 Chapter 9. Adding Location Info to the Selfies 191 Updating the Model 191 Testing Our New Model 194 Showing a Selfie’s Location 195 Expanding the Map 199 Determining Locations 202 Configuring the App to Support Location 202 Talking to the Location Hardware 204 Chapter 10. Building a Settings View 209 Building the Settings UI 209 Connecting the Settings into Our Hierarchy 211 Hooking Up the Settings 212 Implementing the Settings 213 Chapter 11. Reminders and Notifications 217 Adding a Reminder to the Settings 217 Creating a Notification 219 Part III. Polishing Selfiegram 227 Chapter 12. Theming and Sharing Selfiegram 229 Custom Fonts 230 Making the Theme 233 Sharing Selfies 237 Sharing from the Detail View Controller 238 Sharing from the List View Controller 240 Chapter 13. Custom Views and View Controllers 243 A Camera View 244 The Camera View Controller 247 Building the UI 247 Connecting the UI 250 Talking to the Camera 250 Calling the Capture View Controller 256 Chapter 14. Image Overlays 261 Creating the Overlay Model 261 Testing the Overlay Manager 268 Writing the Tests 268 Returning Available Overlays 271 Downloading Overlay Information 272 Downloading Overlay Images 274 Chapter 15. Overlay UI 279 Building the UI 279 Hooking Up the UI 282 Creating the Overlay View 282 Presenting the Overlays 284 Initial Setup 284 Drawing Eyebrows 287 Vision and Image Detection 290 Connecting It into the App 295 Chapter 16. Localization and Internationalization 299 Internationalization 300 Generating the String Table 302 Localization 303 Translating the Strings 306 Testing Locales 306 Pseudolanguages 308 Previewing Localizations 310 Conclusion 312 Part IV. Beyond Developing Selfiegram 313 Chapter 17. Debugging 315 The Debugger 315 Breakpoints 316 Inspecting Code 320 Controlling Program Flow 323 Performance 325 Instruments 326 Chapter 18. UI Tests 329 UI Testing Classes 329 Writing UI Tests 330 A Basic Test 330 Recording UI Actions 331 Checking if Elements Exist 334 Handling UI Interruptions 335 Chapter 19. Automating Chores with Fastlane 337 The Fastlane Tools 338 Installing Fastlane 339 Installing via Homebrew 340 Installing via RubyGems 340 Installing via Direct Download 340 Setting Up a Project 340 Doing a Build 341 Configuring a Fastfile 343 Chapter 20. Using Fastlane’s Tools 345 Code Signing with match 345 The Problem That Match Solves 345 match’s Solution 348 Generating Screenshots Using snapshot 349 Using Boarding to Add Testers to TestFlight 353 Managing Your App’s Presence on the App Store Using deliver 355 Getting the Metadata 356 Submitting New Metadata 356 Looking Further 357 Index 359 About the Authors 377 Colophon 377 Dive into Swift 4.x, the latest version of Apple's easy-to-use programming language for macOS, iOS, watchOS, and tvOS. With this practical guide, you'll quickly get up to speed on language basics and then use Swift to build three complete apps.