Chapter 13

Expanding Objects with Extensions

In This Chapter

arrow Looking into a Swift extension

arrow Extending a built-in type with a Swift extension

arrow Using a complex extension

In many object-oriented languages, adding subclasses (or subclassing) is the most common way of expanding or reusing a class. Over the years, however, additional techniques to expand classes have become common as more and more people use object-oriented programming techniques.

When it comes to reusing code and expanding classes, subclassing is now seen as a pretty blunt instrument. With subclassing, a common approach is to start from a basic class (sometimes called a root or base class), and to subclass it to add new functionality or customize existing functionality. The root class is often an abstract class that is not designed to be implemented. In an oft-used example, the root class might be building, and its first-level subclasses might be residential building and commercial building. You could then subclass residential building to single-family and multi-family. On and on you go, subclassing and subclassing until you have the specific class you need — perhaps a multi-family residential building consisting of ...

Get Swift For Dummies now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.