- Object-Oriented Programming and Swift
- The Terminology of Object-Oriented Development
- Exploring the Swift File Structure
- Swift Programming Basics
- Memory Management and Automatic Reference Counting
- Introducing the iOS Playground
- Further Exploration
- Summary
- Q&A
- Workshop
- Activities
The Terminology of Object-Oriented Development
OOP brings with it a whole range of terminology that you need to get accustomed to seeing in this book (and in Apple’s documentation). The more familiar you are with these terms, the easier it will be to look for solutions to problems and interact with other developers. Let’s establish some basic vocabulary now:
- Class: The code, usually consisting of a single Swift file, which defines an object and what it can do.
- Subclass: A class that builds upon another class, adding additional features. Almost everything you use in iOS development will be a subclass of something else, inheriting all the properties and capabilities of its parent class.
- Superclass/parent class: The class that another class inherits from.
- Singleton: A class that is instantiated only once during the lifetime of a program. For example, a class to read your device’s orientation is implemented as a singleton because there is only one sensor that returns this information.
- Object/instance: A class that has been invoked and is active in your code. Classes are the code that makes an object work, whereas an object is the actual class “in action.” This is also known as an instance of a class.
- Instantiation: The process of creating an active object from a class.
- Instance method: A basic piece of functionality, implemented in a class. For the reminder class, this might be something like setAlarm to set the alarm for a given reminder. Methods are, by default, available within the class they are defined and within other classes defined in the same project.
- Extensions: Provide a means of extending a class without modifying the class code itself.
- Type method: Similar to an instance method, but applicable to all the objects created from a class. The reminder class, for example, might implement a type method called countReminders that provides a count of all the reminder objects that have been created. If you’re familiar with other OO languages, you may recognize this as a static method or a class method.
- Variable property: A storage place for a piece of information specific to a class. The name of a reminder, for example, might be stored in a variable property. All variables have a specific “type” that describes the contents of what they will be holding. Variable properties only differ from normal variables in where they are defined and where they can be accessed.
- Variable: A storage location for a piece of information. Unlike variable properties, a “normal” variable is accessible only in the method where it is defined.
- Constant: A Swift constant is another type of variable, but one that cannot be modified after it has been declared.
- Parameter: A piece of information that is provided to a method when it is use. If you were to use a setAlarm method, you would presumably need to include the time to set. The time, in this case, would be a parameter.
- Protocol: Protocols declare methods that can be implemented by a class—usually to provide functionality needed for an object. A class that implements a protocol is said to conform to that protocol. This is similar to a Java interface.
- Self: A way to refer to an object within its own methods. When an instance method or variable property is used in an application, it should be used with a specific object. If you’re writing code within a class and you want it to access one of its own methods or variable properties, you can self to refer to the object. In Swift, self is usually implied and only needs to be used explicitly in very specific circumstances.
It’s important to know that when you develop for iOS you’re going to be taking advantage of hundreds of classes that Apple has already written for you. Everything from creating onscreen buttons to manipulating dates and writing files is covered by prebuilt classes. You’ll occasionally want to customize some of the functionality in those classes, but you’ll be starting out with a toolbar already overflowing with functionality.
Confused? Don’t worry! This book introduces these concepts slowly, and you’ll quickly get a feel for how they apply to your projects as you work through several tutorials in the upcoming hours.
What Is Swift?
For years, Apple development has centered on a decades-old language called Objective-C. Objective-C, while appealing to some, was about as far from a “modern” language as you could get. Languages like Python and Ruby have sprung up and attracted legions of followers with their simple syntax and focus on results, rather then esoteric concepts like memory management. Swift is Apple’s answer to the call for a modern iOS and OS X development language.
Released in 2014, Swift carries with it many of the niceties of Objective-C, but loses much of the baggage. The biggest issue with Swift is that it is still evolving, and developers (including yours truly) are still trying to figure out the best way to use it. It will be several years before this churn settles down—but, in the meantime, the core of Swift is fast, flexible, and easy to learn.
Swift statements are easier to read than other programming languages and can often be deciphered just by looking at them. For example, code that checks to see if two dates are equal might be written like this:
if
myBirthday.isEqualToDate
(yourBirthday) {// We're the same age!
}
It doesn’t take a very large mental leap to see what is going on in the code snippet. Throughout the book, I will try to explain what each line of code is doing—but chances are you can pick up on the intention just by reading the lines.
Now that you have an idea of what OOP and Swift are, let’s take a look at how you’ll be using them over the course of this book.