Chapter 3. Encapsulation of Data with Properties

In this chapter, you will learn about all the elements that might compose a class. We will start organizing data in blueprints that generate instances. We will work with examples to understand how to encapsulate and hide data by working with properties combined with access control. In addition, you will learn about properties, methods, and mutable versus immutable classes.

Understanding elements that compose a class

So far, we have worked with a very simple class and many instances of this class in the Playground, the Swift REPL and the web-based Swift Sandbox. Now, it is time to dive deep into the different members of a class.

The following list enumerates the most common element types that you can include in a class definition in Swift and their equivalents in other programming languages. We have already worked with a few of these elements:

  • Initializers: These are equivalent to constructors in other programming languages
  • Deinitializers: These are equivalent to destructors in other programming languages
  • Type properties: These are equivalent to class fields or class attributes in other programming languages
  • Type methods: These are equivalent to class methods in other programming languages
  • Subscripts: These are also known as shortcuts
  • Instance properties: These are equivalent to instance fields or instance attributes in other programming languages
  • Instance methods: These are equivalent to instance functions in other programming languages
  • Nested types: These are types that only exist within the class in which we define them

You have already learned how basic initializers and deinitializers work in the previous chapter. So far, we have used an instance-stored property to encapsulate data in our instances. We could access the instance property without any kind of restrictions as a variable within an instance.

However, as it happens sometimes in real-world situations, restrictions are necessary to avoid serious problems. Sometimes, we want to restrict access or transform specific instance properties into read-only attributes. We can combine the restrictions with computed properties that can define getters and/or setters.

Tip

Computed properties can define get and/or set methods, also known as getters and setters. Setters allow us to control how values are set, that is, these methods are used to change the values of related properties. Getters allow us to control the values that we return when computed properties are accessed. Getters don't change the values of related properties.

Sometimes, all the members of a class share the same attribute, and we don't need to have a specific value for each instance. For example, superhero types have some profile values, such as the average strength, average running speed, attack power, and defense power. We can define the following type properties to store the values that are shared by all the instances: averageStrength, averageRunningSpeed, attackPower, and defensePower. All the instances have access to the same type properties and their values. However, it is also possible to apply restrictions to their access.

It is also possible to define methods that don't require an instance of a specific class to be called; therefore, you can invoke them by specifying both the class and method names. These methods are known as type methods, operate on a class as a whole, and have access to type properties, but they don't have access to any instance members, such as instance properties or methods, because there is no instance at all. Type methods are useful when you want to include methods related to a class and don't want to generate an instance to call them. Type methods are also known as static or class methods. However, we have to pay attention to the keyword we use to declare type methods in Swift because a type method declared with the static keyword has a different behavior from a type method declared with the class keyword. We will understand the differences between these as we move forward with the examples in this and forthcoming chapters.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.195.56