Javatpoint Logo
Javatpoint Logo

Swift Properties

We can define properties to associate values with a class, structure, and enumerations. In swift, we can define two types of properties as Stored property and computed property. However, the stored property store variables and constant values as the class attributes. On the other hand, the computed properties calculate values based on a particular value. We can define computed properties in Classes, Structures, and Enumerations; however, the stored properties can only be defined in classes and structures.

The instance of a particular type, including Structure, Class, and Enum, is associated with all the properties defined in the type. However, instead of instances, the properties can also be associated with types only. Such properties are called Type properties.

In this section of the tutorial, we will discuss the various type of swift properties like Stored and Computed properties.

Stored Properties

In simple words, the Stored Property is the constant or variable defined as the part of an instance of a particular Class or Structure. The stored properties are either constant (defined using let keyword) or variable (defined using var keyword). We can give the default values to stored properties at the time of declaring it. However, we can modify the stored property's default value later when initializing the Class or Structure.

Let's consider the following example, which defines a class as a Person. It contains variable stored properties as name and age. Every person has a default name and age, which can be changed later while initializing it.

Now, if we create an instance of the Person class and mark it as constant, it doesn't affect our variable stored properties as we can still change their values, as shown below.

This prints the following output on the console.

"John 23"

However, the same is not true for Structure types as structure are value types. If we mark a structure as constant, we can't change all of its stored properties despite the property being a variable. Consider the following example.

Lazy Stored Property

A lazy stored property doesn't occupy memory in the system until the first time it is used. In other words, the value of the lazy stored property is not calculated until the first time it is accessed. We can use the "lazy" keyword in swift to define a property as lazy. However, we can't define lazy stored property as constant since its value might not be retrieved at the time of declaration.

Lazy properties are useful in the scenario where the value of our property depends on the outside factors and needs to be calculated once those values are known. We can also use lazy properties if the property requires complex setup while initialization and is not needed until the value is accessed the first time.

Consider the following example, which defines the relationship between classes Employee and Department. Here, the class Department contains a lazy stored property of type Employee indicating that the Employee property will be created whenever the Department will be associated with the Employee.

Computed Properties

We can define computed properties as the part of the instance of Classes, Structure, and Enum types. Unlike stored properties, the computed properties don't store the values. Instead, they provide getter and optional setter to retrieve and set other properties and values indirectly.

Example 1

Consider the following example that defines a class Person. The Person has a name, age, and a special message for the world. While the name and age are stored properties, the message is a computed property whose value is determined based on Person's name and age.

The class Person includes stored properties like name, age, and festival for which the person wishes. However, the message is being calculated. Therefore, it is a computed property whose value is based on a person's name, age, and festival for which the message will be delivered.

If we print the person's message, the following message will be printed on the console.

"Hi I am John, and I am 23 years old. I wish you all a very Happy Diwali"

Example 2:

Consider the following example where the class Circle contains two properties. The radius is the stored property, whereas; the area is computed depending upon the circle's radius.

It prints the following on the console.

"Area of circle is 31400.0"

Example 3:

Let's extend the Circle class defined in example 2 and calculate the radius if the circle area is given.

If we assign some value for the circle's area, it will calculate the radius according to the area and assign it to the stored property radius.

It will print the new radius of the circle based on the modified area, as shown below.

"Radius of Circle is 316.2287731100305"

Next TopicSwift Inheritance

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA