Our Type Choices
In most traditional, object-oriented programming languages, we create classes (which are reference types) as blueprints for our objects. In Swift, unlike other object-oriented languages, structures have much of the same functionality as classes; however, they are value types. Apple has said that we should prefer value types, such as structures, to reference types, but what are the actual advantages? Swift actually has a number of type choices that we can use, and in this chapter we will look at all of these types to see their advantages and disadvantages. Knowing how and when to use each type is important in order to properly implement protocol-oriented programming in our projects.
In this chapter, you will learn the following:
- What a class is and how to use it
- What a structure is and how to use it
- What an enumeration is and how to use it
- What a tuple is and how to use it
- The differences between value and reference types
Swift classifies types as either named or compound types. A named type is a type that can be given a name when it is defined. These named types include classes, structures, enumerations, and protocols. In addition to user-defined named types, Swift also defines many commonly-used named types within the Swift standard library, including arrays, sets, and dictionaries.
Many data types that we would normally consider primitive types in other languages are actually named types in Swift and are implemented in the Swift standard library using structures. These include types that represent numbers, strings, characters, and Boolean values. Since these types are implemented as named types, we are able to extend their behavior using extensions as we would with any other named type. As we will see in both this and future chapters, the ability to extend a named type, including types that would traditionally be considered as primitive types and protocols, is an extremely powerful feature of the Swift language and is one of the pillars of protocol-oriented programming. We will look at extensions in Chapter 3, Extensions.
A compound type is a type that is not given a name when it is defined. In Swift, we have two compound types: function types and tuple types. Function types represent closures, functions, and methods, while tuple types take the form of a comma-separated list that is enclosed in parentheses. We are able to use the typealias declaration to give an alias to compound types. This allows us to use the alias name instead of the type itself within our code.
There are two categories of types: reference types and value types. When we pass an instance of a reference type, we are passing a reference to the original instance, which means that the two references are sharing the same instance. Classes are reference types.
When we pass an instance of a value type, we are passing a new copy of the instance, which means that each instance gets a unique copy. Value types include structures, enumerations, and tuples.
Every type in Swift will be either a named or compound type, and they will also be either a reference or value type, except in the case of protocols. Since we are unable to create an instance of a protocol, it is neither a reference nor a value type. Sounds a bit confusing? It really isn't. As we look at all type choices and how we can use them, we will see how easy this is to understand.
Now, let's begin looking at the type choices that we have in Swift. We will begin by looking at the backbone of object-oriented programming: the class.