iOS 12 Programming Fundamentals with Swift, Fifth Edition
by Matt Neuburg
Copyright 2018 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: Karen Montgomery
- Interior Designer: David Futato
- Illustrator: Matt Neuburg
- April 2015: First Edition
- October 2015: Second Edition
- October 2016: Third Edition
- October 2017: Fourth Edition
- September 2018: Fifth Edition
Revision History for the Fifth Edition
- 2018-09-26: First release
See http://oreilly.com/catalog/errata.csp?isbn=9781492044550 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. iOS 12 Programming Fundamentals with Swift, the image of a harp seal, and related trade dress are trademarks of OReilly 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-492-04455-0
[LSI]
Preface
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 consider 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 Swift deserved to be, and probably would be, adopted by new students of iOS programming, and that my books, therefore, should henceforth assume that readers are using Swift.
That decision has proven prophetic. Programmers of iOS have flocked to Swift in increasing numbers, and Swift itself has only improved. My iOS apps (such as Diabellis Theme, LinkSame, Zotz!, TidBITS News, and my Latin and Greek flashcard apps) have all been rewritten in Swift, and are far easier for me to understand and maintain than their Objective-C originals.
Xcode 10 comes with Swift 4.2. The language has evolved greatly in its details and in the nature of its integration with the Cocoa libraries that underlie iOS programming, but its spirit has remained constant.The Swift language is designed from the ground up with these salient features:
Object-orientation
Swift is a modern, object-oriented language. It is purely object-oriented: Everything is an object.
Clarity
Swift is easy to read and easy to write. Its syntax is clear, consistent, and explicit, with few hidden shortcuts and minimal syntactic trickery.
Safety
Swift enforces strong typing to ensure that it knows, and that you know, what the type of every object reference is at every moment.
Economy
Swift is a fairly small language, providing some basic types and functionalities and no more. The rest must be provided by your code, or by libraries of code that you use such as Cocoa.
Memory management
Swift manages memory automatically. You will rarely have to concern yourself with memory management.
Cocoa compatibility
The Cocoa APIs are written primarily in C and Objective-C. Swift is explicitly designed to interface with most of the Cocoa APIs.
These features make Swift an excellent language for learning to program iOS.
The alternative, Objective-C, still exists, and you can use it if you like. Indeed, it is easy to write an app that includes both Swift code and Objective-C code; and you may have reason to do so. Objective-C, however, lacks the very advantages that Swift offers. Objective-C agglomerates object-oriented features onto C. It is therefore only partially object-oriented; it has both objects and scalar data types, and its objects have to be slotted into one particular C data type (pointers). Its syntax can be difficult and tricky; reading and writing nested method calls can make ones eyes glaze over, and it invites hacky habits such as implicit nil-testing. Its type checking can be and frequently is turned off, resulting in programmer errors where a message is sent to the wrong type of object and the program crashes.
Recent revisions and additions to Objective-C ARC, synthesis and autosynthesis, improved literal array and dictionary syntax, blocks have made it easier and more convenient, but such patches have also made the language even larger and possibly even more confusing. Because Objective-C must encompass C, there are limits to how far it can be extended and revised. Swift, on the other hand, is a clean start. If you were to dream of completely revising Objective-C to create a better Objective-C, Swift might be what you would dream of. It puts a modern, rational front end between you and the Cocoa Objective-C APIs.
Still, the reader may 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 might have to know what those languages would expect.
Therefore, although I do not attempt to teach Objective-C in this book, I do describe it in enough detail to allow you to read it when you encounter it in the documentation and on the Internet, and I occasionally show some Objective-C code. , on Cocoa, is really all about learning to think the way Objective-C thinks because the structure and behavior of the Cocoa APIs are fundamentally based on Objective-C. And the book ends with an appendix that details how Swift and Objective-C communicate with one another, as well as explaining how your app can be written partly in Swift and partly in Objective-C.
The Scope of This Book
This book is actually one of a pair with my Programming iOS 12, which picks up exactly where this book leaves off. They complement and supplement one another. The two-book architecture should, I believe, render the size and scope of each book tractable for readers. Together, they provide a complete grounding in the knowledge needed to begin writing iOS apps; thus, when you do start writing iOS apps, youll have a solid and rigorous understanding of what you are doing and where you are heading. If writing an iOS program is like building a house of bricks, this book teaches you what a brick is and how to handle it, while Programming iOS 12 hands you some actual bricks and tells you how to assemble them.
When you have read this book, youll know about Swift, Xcode, and the underpinnings of the Cocoa framework, and you will be ready to proceed directly to