Preface

Welcome to Learning Swift! This book will help you put the Swift programming language into practice by walking you through the development of a note-taking application for the Apple iOS, macOS, and watchOS platforms.

Swift is a pretty amazing modern language, taking the best from other newer languages without reinventing the wheel. Swift is easy to write, easy to read, and really hard to make mistakes in.

Our philosophy is that the best way to learn Swift is to build apps using it! To build apps, though, you need a great framework, and Apple has several: Cocoa, Cocoa Touch, and WatchKit, to name only a few. This book could quite easily be titled Learning Cocoa and Cocoa Touch with Swift, or something similar, because the frameworks are just as important as the language itself. At the time of writing, Swift is currently at version 3, and has a bright future ahead of it.

Resources Used in This Book

We recommend following the book by writing code yourself as you progress through each chapter. If you get stuck, or just want to archive a copy of the code, you can find what you need via our website.

As this book teaches you how to build a real-world app, we primarily focus on showing you the coding side of things. We’re not going to ask you to paint your own icons, so we’ve provided them for you. You can also download them from our website.

Audience and Approach

This book is solely focused on Swift 3 and does not cover the use of Objective-C. We might mention it occasionally, but we don’t expect you to know how to use it. We first cover the basics of the Swift 3 language, and then move on to teach as much of the language as we can, as well as the use of the Cocoa, Cocoa Touch, and watchOS frameworks, through the construction of a complete app for both macOS and iOS. As a reminder, Swift is the programming language, Cocoa is the framework for macOS apps, Cocoa Touch is the framework for iOS apps, and somewhat predictably, watchOS is the framework for the Apple Watch.

This book’s approach differs from that of other programming books that you may have encountered. As we’ve mentioned, we believe that the best way to learn Swift is to build apps using it. We assume that you’re a reasonably capable programmer, but we don’t assume you’ve ever developed for iOS or macOS, or used Swift or Objective-C before. We also assume that you’re fairly comfortable navigating macOS and iOS as a user.

Organization of This Book

In this book, we’ll be talking about Cocoa and Cocoa Touch, the frameworks used on macOS and iOS, respectively. Along the way, we’ll also be covering Swift, including its syntax and features.

In Part I, “Swift Basics”, we begin with a look at the tools used for programming with Swift, as well as the Apple Developer Program. Then we move on to the basics of the Swift programming language and structuring a program for Apple’s platforms, as well as common design patterns.

Chapter 1 covers the basics of Apple’s developer program and guides you through a simple Swift app.

Chapter 2 explores all the basics of Swift and prepares you for using it to build more complex applications.

Chapter 3 discusses Swift’s object-oriented features, as well as the structure of a good app.

In Part II, “A macOS App”, we build a simple note-taking application for Macs, targeting macOS. Along the way, we discuss the design of the app, how it’s structured, how it uses documents, and how to build all the features.

Chapter 4 starts off our macOS notes app and sets up the document model and icon.

Chapter 5 goes into detail on working with documents in macOS apps.

Chapter 6 connects the app to iCloud and finishes up the macOS app.

In Part III, “An iOS App”, we build a fully featured iOS note-taking application as a companion for the macOS app from Part II.

Chapter 7 starts off our iOS app and sets up the same document model for iOS.

Chapter 8 connects the iOS app to iCloud.

Chapter 9 creates an interface on iOS for displaying our notes.

Chapter 10 sets up the iOS app to handle attachments.

Chapter 11 adds image support to the iOS app.

Chapter 12 adds sharing and searching support to the iOS app.

Chapter 13 adds audio, video, and location attachments to the iOS app.

Chapter 14 finishes the iOS app with a whole lot of polish!

In Part IV, “Extending Your Apps”, we add a watchOS app and explore bug hunting and performance tuning.

Chapter 15 adds a watchOS app to the iOS app, allowing for Apple Watch support.

Chapter 16 explores debugging and performance tuning.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Tip

This element signifies a tip or suggestion.

Note

This element signifies a general note.

Caution

This element indicates a warning or caution.

Using Code Examples

Supplemental material (code examples, exercises, errata, etc.) is available for download at our website.

This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Learning Swift, Second Edition by Jonathon Manning, Paris Buttfield-Addison, and Tim Nugent (O’Reilly). Copyright 2017 Secret Lab, 978-1-491-96706-5.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

O’Reilly Safari

Note

Safari (formerly Safari Books Online) is a membership-based training and reference platform for enterprise, government, educators, and individuals.

Members have access to thousands of books, training videos, Learning Paths, interactive tutorials, and curated playlists from over 250 publishers, including O’Reilly Media, Harvard Business Review, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Adobe, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, and Course Technology, among others.

For more information, please visit http://oreilly.com/safari.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/learning-swift.

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

Jon thanks his mother, father, and the rest of his crazily extended family for their tremendous support.

Paris thanks his mother, without whom he wouldn’t be doing anything nearly as interesting, let alone writing books.

Tim thanks his parents and family for putting up with his rather lackluster approach to life.

We’d all like to thank our editors, Rachel Roumeliotis and Brian MacDonald—their skill and advice were invaluable to completing the book. Likewise, all the O’Reilly Media staff we’ve interacted with over the course of writing the book have been the absolute gurus of their fields.

A huge thank you to Tony Gray and the Apple University Consortium (AUC) for the monumental boost they gave us and others listed on this page. We wouldn’t be writing this book if it weren’t for them. And now you’re writing books, too, Tony—sorry about that!

Thanks also to Neal Goldstein, who deserves full credit and/or blame for getting us into the whole book-writing racket.

We’re thankful for the support of the goons at MacLab (who know who they are and continue to stand watch for Admiral Dolphin’s inevitable apotheosis), as well as professor Christopher Lueg, Dr. Leonie Ellis, and the rest of the staff at the University of Tasmania for putting up with us. “Apologies” to Mark Pesce. He knows why.

Additional thanks to Rex S., Nic W., Andrew B., Jess L., and Ash J., for a wide variety of reasons. And very special thanks to Steve Jobs, without whom this book (and many others like it) would not have reason to exist.

Thanks also to our tech reviewers, with special thanks to Chris Devers and Tony Gray for their thoroughness and professionalism.

Finally, thank you very much for buying our book—we appreciate it! And if you have any feedback, please let us know. You can email us at lab@secretlab.com.au and find us on Twitter at @thesecretlab.

Get Learning Swift, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.