iOS 15 Programming Fundamentals with Swift, Eighth Edition
by Matt Neuburg
Copyright 2022 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 | Indexer: Matt Neuburg |
Production Editor: Kristen Brown | Cover Designer: Karen Montgomery |
Proofreader: OReilly Production Services | 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
- October 2020: Seventh Edition
- November 2021: Eighth Edition
Revision History for the Early Release
- 2021-09-22: First Early Release
See http://oreilly.com/catalog/errata.csp?isbn=9781098118501 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. iOS 15 Programming Fundamentals with Swift, the image of a harp seal, and related trade dress are trademarks of OReilly Media, Inc.
The views expressed in this work are those of the author, and do not represent the publishers views. 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-098-11850-1
[LSI]
Preface
In July of 2010, Chris Lattner created a folder on his computer called Shiny, and a new computer language was born. Four years later, in 2014, that language, renamed Swift, was introduced to the public, and was greeted with a mixture of surprise and excitement and skepticism.
Prior to that moment, Cocoa programming, on iOS and before that on Mac OS, had always been done chiefly in Objective-C. The Cocoa frameworks that give an iOS app its functionality are based on Objective-C; they expect to be spoken to in Objective-C. The tradition of using Objective-C was long and deeply ingrained. For all its faults, Objective-C was the language we had learned to live with as the price of programming Cocoa. Could Cocoa be spoken to in a whole new language? Could this new language replace Objective-C as the iOS developers language of choice?
No one knew. I certainly didnt know! So the first thing I did, as an experiment, was to try translating my own existing iOS apps into Swift. Not only was I able to do it, but I found the new Swift versions easier to understand and maintain than their Objective-C originals. From that moment, I was convinced that the vast majority of new iOS programmers would hitherto adopt Swift. I was right.
Swift is a superb language to learn, even (perhaps especially) if youve never programmed before, and is the easiest and clearest way to program iOS. It 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.
Earlier editions of this book, before 2014, taught the reader Objective-C. After 2014, they teach Swift. This edition is geared to Swift 5.5. The Swift language has reached a high state of maturity. It has achieved ABI stability, which means that the Swift language has become part of the system. Swift apps are smaller and faster than ever.
The Foundation and Cocoa APIs, however, are still written in C and Objective-C. 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 doesnt teach you how to write any practical iOS programs. Its about the fundamental underpinnings of iOS programming, the basics that you need to understand from the outset in order to get started. 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; actually assembling the bricks comes later. In this book, youll learn all about:
The programming language youll be writing in (Swift).
The milieu in which youll be writing, editing, running, testing, and generally developing (Xcode).
The framework youll be communicating with as your app runs (Cocoa).
When you have read this book, youll know about Swift, Xcode, and the nature of the Cocoa framework, and you will be ready to proceed to learn about real-life iOS app components such as views and view controllers.And even though the book doesnt show how to write any particularly interesting iOS apps, 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.
The book has three parts:
introduces the Swift language, from the ground up I do not assume that you know any other programming languages. My way of teaching Swift is different from other treatments, such as Apples; it is systematic and Euclidean, with pedagogical building blocks piled on one another in what I regard as the most helpful order. At the same time, I have tried to confine myself to the essentials. Swift is not a big language, but it has some subtle and unusual corners that you probably dont need to know about.Also, I never mention Swift playgrounds or the REPL. 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.