Swift Properties

In this tutorial you will learn about the Swift Properties and its application with practical example.

Swift Properties

Properties is way to associate an attribute and its value with a particular class, structure, or enumeration.Properties can be a constant or variable that stores associated values for classes, structures or enumerations. In swift, properties have getter and setter methods to set, modify or retrieve property value.

In Swift, properties can classified into following two types –

  1. Stored properties
  2. Computed properties

Swift Stored Properties

The stored properties are simply a constant or variable that is being associated and stored as part of an instance of any specific class or structure. The stored properties can be a variable or constant stored properties that can be declared using var or let keyword with an initial value.

Example:-

Output:-

swift_stored_properties

Swift Lazy Stored Properties

In Swift we have a flexible property type called as lazy stored properties whose initial values is not evaluated until it is accessed or used for the first time. Lazy stored properties are useful when we want to hold initialization for some complex computations or when initial value of property to dependent upon some external factors. In swift a property can be declared as lazy stored property using lazy keyword just before its declaration.

Example:-

Output:-

swift_lazy_stored_properties

 

Swift Computed Properties

In swift unlike stored properties computed properties calculates value at the run time rather than to store.Computed properties have a getter to retrieve the value and an optional setter to initialize values.Computed properties can be defined for classes, structures, and enumerations whereas stored properties are defined for classes and structures.

Computed properties can also be declared using var keyword, but rather than an initial value we assign a function to calculate the value.

Example:-

Output:-

swift_computed_properties

Swift Read-Only Computed Properties

The Read-Only computed property are computed property with only getter but no setter. Read-Only computed property are always mean to return a value and can be accessed through dot (.) syntax.

Example:-

Output:-

swift_read_only_computed_properties

Swift Property Observer

In swift property observers observe and responds to changes in a property’s value. Property observer is invoked every time property’s value changes or set to new value, even if the new values is same as current value.Property observers can be added to any stored properties except lazy stored properties, it can also be added to any inherited property by overriding the property within a subclass.

In swift, we have following two types of observers.

  • willSet – It is called just before the value is stored.
  • didSet – It is called after a new value is stored.

Example:-

Output:-

swift_property_observer

Swift Type Property

In Swift, Instance properties are associated with individual instance of a class or structure. Every a new instance created it has separate copy of property values from other instances.In Swift, we can define a special kind of properties that is associated to the class or structure itself, rather than any individual instance no matter how many instances have been created, there will always one copy of these properties will available for all instances of that type.These properties are termed as type properties.Type properties can be defined using ‘static‘ keyword and for class types ‘class‘ keyword is used.

Example:-

Output:-

swift_type_properties

In this tutorial we have learn about the Swift Properties and its application with practical example. I hope you will like this tutorial.