Programming iOS 10, Seventh Edition
by Matt Neuburg
Copyright 2017 Matt Neuburg. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly 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/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Editor: Rachel Roumeliotis
- Production Editor: Kristen Brown
- Proofreader: OReilly Production Services
- Indexer: Matt Neuburg
- Cover Designer: Randy Comer
- Interior Designer: David Futato
- Illustrator: Matt Neuburg
- May 2011: First Edition
- March 2012: Second Edition
- March 2013: Third Edition
- December 2013: Fourth Edition
- December 2014: Fifth Edition
- November 2015: Sixth Edition
- November 2016: Seventh Edition
Revision History for the Seventh Edition:
- 2016-11-08: First release
See http://oreilly.com/catalog/errata.csp?isbn=9781491970164 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Programming iOS 10, the image of a kingbird, and related trade dress are trademarks of OReilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and OReilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps.
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-97016-4
[LSI]
Preface
Aut lego vel scribo; doceo scrutorve sophian.
Sedulius Scottus
On June 2, 2014, Apples WWDC keynote address ended with a shocking announcement: We have a new programming language. This came as a huge surprise to the developer community, which was accustomed to Objective-C, warts and all, and doubted that Apple could ever possibly relieve them from the weight of its venerable legacy. The developer community, it appeared, had been wrong.
Having picked themselves up off the floor, developers immediately began to examine this new language Swift studying it, critiquing it, and deciding whether to use it. My own first move was to translate all my existing iOS apps into Swift; this was enough to convince me that, for all its faults, Swift deserved to be adopted by new students of iOS programming, and that my books, therefore, should henceforth assume that readers are using Swift.
Therefore, Swift is the programming language used throughout this book. Nevertheless, the reader will also need some awareness of Objective-C (including C). The Foundation and Cocoa APIs, the built-in commands with which your code must interact in order to make anything happen on an iOS device, are still written in C and Objective-C. In order to interact with them, you have to know what those languages would expect. For example, in order to pass a Swift array where an Objective-C NSArray is expected, you need to know whats required of an object to be acceptable as an element of an Objective-C NSArray.
If you dont already know the Swift language and how it interacts with Objective-C, youll want to start with my other book, iOS 10 Programming Fundamentals with Swift. It is both an introduction and a companion to Programming iOS 10. Like Homers Iliad, Programming iOS 10 begins in the middle of the story, with the reader jumping with all four feet into views and view controllers, and with a knowledge of the language and the Xcode IDE already presupposed. Discussion of the programming language, as well as the Xcode IDE (including the nature of nibs, outlets, and actions, and the mechanics of nib loading), plus the fundamental conventions, classes, and architectures of the Cocoa Touch framework (including delegation, the responder chain, keyvalue coding, memory management, and so on) material that constituted Chapters 113 in the early editions of this book, but whose presence was eventually deemed to be making the book unwieldy in size and scope has now been relegated to iOS 10 Programming Fundamentals with Swift.
So if something appears to be missing from this book, thats why! If you start reading Programming iOS 10 and wonder about such unexplained matters as Swift language basics, the UIApplicationMain
function, the nib-loading mechanism, Cocoa patterns of delegation and notification, and what a retain cycle is, wonder no longer I dont explain them here because I have already explained them in iOS 10 Programming Fundamentals with Swift. If youre not sufficiently conversant with those topics, Id suggest that you might want to read that book first; you will then be completely ready for this one.
The Scope of This Book
Heres a summary of this books major sections:
describes views, the fundamental units of an iOS apps interface. Views are what the user can see and touch in an iOS app. To make something appear before the users eyes, you need a view. To let the user interact with your app, you need a view. This part of the book explains how views are created, arranged, drawn, layered, animated, and touched.
starts by discussing view controllers. Perhaps the most remarkable and important feature of iOS programming, view controllers enable views to come and go coherently within the interface, thus allowing a single-windowed app running on what may be a tiny screen to contain multiple screens of material. This part of the book talks about all the ways in which view controllers can be manipulated in order to make their views appear. It also describes every kind of view provided by the Cocoa framework the built-in building blocks with which youll construct an apps interface.
surveys the most important secondary frameworks provided by iOS. These are clumps of code, sometimes with built-in interface, that are not part of your app by default, but are there for the asking if you need them, allowing you to work with such things as sound, video, user libraries, maps, and the devices sensors.
wraps up the book with some miscellaneous but important topics: files, networking, threading, and how to implement undo.
summarizes the most important lifetime event messages sent to your app delegate.
catalogs some useful Swift utility functions. You should keep an eye on this appendix, consulting it whenever a mysterious method name appears. For instance, my example code frequently uses my delay
function, which embraces asyncAfter
with a convenient shorthand; when I use delay
and you dont know what it is, consult this appendix. Similarly, my initializers for CGRect and CGPoint have no labels; thats because I use Swift extensions that define those initializers, and those extensions are given in this appendix.