iOS 14 Programming Fundamentals Swift Book Details
Title: iOS 14 Programming Fundamentals Swift
Author: Matt Neuburg
No. of pages: 709
Format: PDF, Mobile, Source code
Preface IOS 14 Programming Fundamentals with Swift
Swift enforces strong typing to ensure that it knows, and that you know, what the type of every object reference is at every moment.
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.
Swift manages memory automatically. You will rarely have to concern yourself with memory management.
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.3. The Swift language has reached a high state of maturity. It has achieved ABI stability, which means that the Swift lan‐ guage 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. Therefore in this book I describe Objective-C 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. Part III, 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 explain‐ ing how your app can be written partly in Swift and partly in Objective-C.
The Scope of This iOS 14 Programming Fundamentals Swift Book
This book is intended to accompany and precede Programming iOS 14, 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 Program‐ ming iOS 14 shows you some actual bricks and tells you how to assemble them.
When you have read this book, you’ll know about Swift, Xcode, and the underpin‐ nings of the Cocoa framework, and you will be ready to proceed directly to Program‐ ming iOS 14. Conversely, Programming iOS 14 assumes a knowledge of this book; it begins, like Homer’s 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 14 and
wondered about such unexplained matters as Swift language basics, the UIApplicationMain function, the nib-loading mechanism, Cocoa patterns of delega‐ tion and notification, and retain cycles, wonder no longer — I didn’t explain them there because I do explain them here.
This book doesn’t 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:
• Part I 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 differ‐ ent from other treatments, such as Apple’s; 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 don’t 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.
• Part II 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 documenta‐ tion 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 iOS 14 Programming Fundamentals Swift book).
• Part III introduces the Cocoa Touch framework. The Foundation and UIKit frameworks, and other frameworks that they entail, constitute Cocoa, which pro‐ vides 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 Cocoa’s features and behaviors. Cocoa pro‐ vides important foundational classes and adds linguistic and architectural devices such as categories, protocols, delegation, and notifications, as well as the perva‐ sive responsibilities of memory management. Key–value coding and key–value observing are also discussed here.
The last chapter of Part III is about the general problem of how objects can refer to one another in an iOS program. In addition to the traditional Cocoa-based
solutions, I also discuss the new Swift Combine framework. Also, in June of 2019, Apple introduced SwiftUI. It constitutes an alternative to UIKit and Cocoa, with a completely different programming paradigm for constructing apps. I do not teach SwiftUI in this book — that would require another entire book — but I do explain its chief linguistic features, and I talk about its solutions to the prob‐ lem of communicating between objects within an iOS app and how they differ from Cocoa patterns.
From the Preface to the First Edition (Programming iOS 4)
The popularity of the iPhone, with its largely free or very inexpensive apps, and the subsequent popularity of the iPad, have brought and will continue to bring into the fold many new programmers who see programming for these devices as worthwhile and doable, even though they may not have felt the same way about OS X. Apple’s own annual WWDC developer conventions have reflected this trend, with their emphasis shifted from OS X to iOS instruction.
The widespread eagerness to program iOS, however, though delightful on the one hand, has also fostered a certain tendency to try to run without first learning to walk. iOS gives the programmer mighty powers that can seem as limitless as imagination itself, but it also has fundamentals. I often see questions online from programmers who are evidently deep into the creation of some interesting app, but who are sty‐ mied in a way that reveals quite clearly that they are unfamiliar with the basics of the very world in which they are so happily cavorting.
It is this state of affairs that has motivated me to write this iOS 14 Programming Fundamentals Swift book, which is intended to ground the reader in the fundamentals of iOS. Here I have attempted to marshal and expound, in what I hope is a pedagogically helpful and instructive yet ruthlessly Euclidean and logical order, the principles and elements on which sound iOS pro‐ gramming rests. My hope, as with my previous books, is that you will both read this book cover to cover (learning something new often enough to keep you turning the pages) and keep it by you as a handy reference.
This iOS 14 Programming Fundamentals Swift book is not intended to disparage Apple’s own documentation and example projects. They are wonderful resources and have become more wonderful as time goes on. I have depended heavily on them in the preparation of this book. But I also find that they don’t fulfill the same function as a reasoned, ordered presentation of the facts. The online documentation must make assumptions as to how much you already know; it can’t guarantee that you’ll approach it in a given order. And online documentation is more suitable to reference than to instruction. A fully written example, no matter how well commented, is difficult to follow; it demonstrates, but it does not teach.
A iOS 14 Programming Fundamentals Swift book, on the other hand, has numbered chapters and sequential pages; I can assume you know views before you know view controllers for the simple reason that Part I precedes Part II. And along with facts, I also bring to the table a degree of expe‐ rience, which I try to communicate to you. Throughout this book you’ll find me referring to “common beginner mistakes”; in most cases, these are mistakes that I have made myself, in addition to seeing others make them. I try to tell you what the pitfalls are because I assume that, in the course of things, you will otherwise fall into them just as naturally as I did as I was learning. You’ll also see me construct many examples piece by piece or extract and explain just one tiny portion of a larger app. It is not a massive finished program that teaches programming, but an exposition of the thought process that developed that program. It is this thought process, more than anything else, that I hope you will gain from reading this book.