iOS 13 Programming Fundamentals with Swift, Fifth Edition
by Matt Neuburg
Copyright 2019 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). 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
- October 2019: Sixth Edition
Revision History for the Sixth Edition
- 2019-07-24: First release
See http://oreilly.com/catalog/errata.csp?isbn=9781492074533 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. iOS 13 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-07453-3
[LSI]
Preface
With the arrival of Swift 5 in early 2019, the stamp of maturity has been placed upon the Swift language. When Swift was introduced to the public in 2014, it was a sort second-class citizen. The Cocoa frameworks that give an iOS app its functionality expect to be spoken to in Objective-C, and several megabytes of libraries had to be included in every Swift app, effectively containing the whole of the Swift language and translating everything into Objective-C. But Swift 5 introduces ABI stability, which means that, starting in iOS 10.2, the Swift language has become part of the system. Swift is now on a par with Objective-C, and Swift apps are smaller and faster.
When Swift first appeared, I immediately translated my own existing iOS apps into Swift, and found them easier to undertand and maintain than their Objective-C originals. Objective-C is a powerful language with some remarkable capabilities, but it is safe to say that the vast majority of new iOS programmers will adopt Swift. It is a superb language to learn, even (perhaps especially) if youve never programmed before, and is the easiest and clearest way to program iOS. Swift has 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.
Even when your code is in Swift, some awareness of Objective-C (including C) can be useful. The Foundation and Cocoa APIs are still written in C and Objective-C. In order to interact with them, you might have to know what those languages would expect., on Cocoa, is largely 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 intended to accompany and precede Programming iOS 13, which picks up where this book leaves off.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 13 shows 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 Programming iOS 13. Conversely, Programming iOS 13 assumes a knowledge of this book; it begins, like Homers Iliad, 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. If you started reading Programming iOS 13 and wondered about such unexplained matters as Swift language basics, the UIApplicationMain
function, the nib-loading mechanism, Cocoa patterns of delegation and notification, and retain cycles, wonder no longer I didnt explain them there because I do explain them here.
This book doesnt show how to write any particularly interesting iOS apps, but it does constantly use my own real apps and real programming situations to illustrate and motivate its explanations, as it teaches you the underlying basis of iOS programming.It has three parts:
. My focus here is real-life iOS programming, and my explanation of Swift concentrates on the practical aspects of the language that actually come into play in the course of programming iOS.
turns to Xcode, the world in which all iOS programming ultimately takes place. It explains what an Xcode project is and how it is transformed into an app, and how to work comfortably and nimbly with Xcode to consult the documentation and to write, navigate, and debug code, as well as how to bring your app through the subsequent stages of running on a device and submission to the App Store. There is also a chapter on nibs and the nib editor (Interface Builder), including outlets and actions as well as the mechanics of nib loading (but such specialized topics as autolayout constraints in the nib are postponed to the other book).
introduces the Cocoa Touch framework. The Foundation and UIKit frameworks, and other frameworks that they entail, constitute Cocoa, which provides the underlying functionality that any iOS app needs to have. To use a framework effectively, you have to think the way the framework thinks, put your code where the framework expects it, and fulfill many obligations imposed on you by the framework. Also, Cocoa uses Objective-C, so you need to know how your Swift code will interface with Cocoas features and behaviors. Cocoa provides important foundational classes and adds linguistic and architectural devices such as categories, protocols, delegation, and notifications, as well as the pervasive responsibilities of memory management. Keyvalue coding and keyvalue observing are also discussed here.